CF - A. Fraction

系统管理员 2022-06-08 03:50 328阅读 0赞

题目连接:http://codeforces.com/contest/854/problem/A

题目描述

Description

Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction is called proper iff its numerator is smaller than its denominator (a < b) and that the fraction is called irreducible if its numerator and its denominator are coprime (they do not have positive common divisors except 1).

During his free time, Petya thinks about proper irreducible fractions and converts them to decimals using the calculator. One day he mistakenly pressed addition button ( + ) instead of division button (÷) and got sum of numerator and denominator that was equal to n instead of the expected decimal notation.

Petya wanted to restore the original fraction, but soon he realized that it might not be done uniquely. That’s why he decided to determine maximum possible proper irreducible fraction such that sum of its numerator and denominator equals n. Help Petya deal with this problem.

Input

In the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction.

Output

Output two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.

Sample Input

3

Sample Output

1 2

Sample Input

4

Sample Output

1 3

Sample Input

12

Sample Output

5 7

解题思路

输入一个数,分成两个数一个做分子a,一个做分母b,a < b,a/b < 1,而且ab互为质数,要求a/b尽可能大。
可以转换成只要ab的最大公因数为1,就互质了。gcd算法求
如果n是偶数的话,分成两个奇数,一定互质,因为两个奇数相近。
偶数的话需要判定。

AC代码

  1. #include<iostream>
  2. using namespace std;
  3. int gcd(int a, int b) {
  4. return b == 0 ? a : gcd(b, a%b);
  5. }
  6. int main() {
  7. int n;
  8. cin >> n;
  9. if(n%2 == 0) {
  10. for(int i=n/2-1, j=n/2+1; i>=1; i--, j++){
  11. if(gcd(i,j) == 1)
  12. {
  13. cout << i << " " << j << endl;
  14. break;
  15. }
  16. }
  17. }else {
  18. cout << (n-1)/2 << " " << (n+1)/2 << endl;
  19. }
  20. return 0;
  21. }

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CF1197A

    CF1197A 题意: > 定义k阶梯子为两边各一块木板长度至少k+1,中间k块木板至少为1 。问 给你n块木板,最多能搭成几阶的梯子。 解法: >...

    相关 CF1206A

    CF1206A 题意: > 给你 $ a , b $ 两个数组,要求从两个数组中各选一个数,使得它们的和不存在于任何一个数组。 解法: > 一道极端...

    相关 CF1200A

    CF1200A 解法: > 给出长度为n的字符串,字符串由'L'、'R'以及数字0~9组成。旅馆有10间房子,L代表客人从左边入住,R代表客人从右边入住,数...

    相关 CF1195A

    CF1195A 题意: > 输入n和k,n是学生的数量,k是饮料种类,接下来的n行会输入每个学生想要的饮料的编号,分配饮料是按一对一对分,每一对都是类型相同...

    相关 CF1207A

    CF1207A-There Are Two Types Of Burgers 题意: > 出售普通汉堡和鸡肉汉堡,并且两种汉堡所需的原材料价格不同,问最多能...

    相关 CF1204A

    CF1204A. BowWow and the Timetable 题意: > 给你一个2进制数,求这个2进制数在10进制中的 $ 4^i $ 的个数。 ...

    相关 CF1081A

    CF1081A > 题意: > > > 从 ? 开始每次减去一个不是 ?的约数的数,问最小能得到多少? > > 做法: > > > 因为 $ n $ 一