Round #433 (Div.2)
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.
In the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction.
Output two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.
3
1 2
4
1 3
12
5 7
题意:给一个数,然后把它分解为两个最大的数,并且这两个数互质
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int gcd(int a,int b){ 5 return b==0?a:gcd(b,a%b); 6 } 7 int main(){ 8 int n; 9 scanf("%d",&n); 10 int ans=1; 11 for(int i=1;i<=n;i++){ 12 int j=n-i; 13 if(i>=j) break; 14 if(gcd(i,j)==1) ans=i; //互质 15 } 16 printf("%d %d\n",ans, n-ans); 17 return 0; 18 }
Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments that are numbered from 1 to n and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale.
Maxim often visits his neighbors, so apartment is good for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly k already inhabited apartments, but he doesn't know their indices yet.
Find out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim.
The only line of the input contains two integers: n and k (1 ≤ n ≤ 109, 0 ≤ k ≤ n).
Print the minimum possible and the maximum possible number of apartments good for Maxim.
6 3
1 3
In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good.
题意:有一个公寓,Maxim想住进去,这个公寓最少已经入住一人,Maxim想和邻居建立和睦的关系,所以他想尽量距离邻居近一点,
把公寓每3份分一下,他最小的可能性是1,这个是固定的,主要是求最大的可能性,
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int main(){ 5 int n,k; 6 scanf("%d%d",&n, &k); 7 if(n==k||k==0) 8 printf("0 0"); 9 else { 10 int xx=n/3; 11 if(k<=xx) printf("1 %d", 2*k); 12 else printf("1 %d", n-k); 13 } 14 return 0; 15 }