Uva10976

Fractions Again?! UVA - 10976

It is easy to see that for every fraction in the form 1 k (k > 0), we can always find two positive integers x and y, x ≥ y, such that: 1 k = 1 x + 1 y Now our question is: can you write a program that counts how many such pairs of x and y there are for any given k?
Input Input contains no more than 100 lines, each giving a value of k (0 < k ≤ 10000).
Output
For each k, output the number of corresponding (x,y) pairs, followed by a sorted list of the values of x and y, as shown in the sample output.
Sample Input
2 12
Sample Output
2 1/2 = 1/6 + 1/3 1/2 = 1/4 + 1/4 8 1/12 = 1/156 + 1/13 1/12 = 1/84 + 1/14 1/12 = 1/60 + 1/15 1/12 = 1/48 + 1/16 1/12 = 1/36 + 1/18 1/12 = 1/30 + 1/20 1/12 = 1/28 + 1/21 1/12 = 1/24 + 1/24

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <queue>
 4 #include <vector>
 5 #include<string.h>
 6 #include<map>
 7 #include<bits/stdc++.h>
 8 #define LL long long
 9 #define maxn 1005
10 using namespace std;
11 //vector<int> m;
12 int main()
13 {
14     int k;
15     while(scanf("%d",&k)!=EOF)
16     //while(~scanf("%d",&k),k)这么写就WA,这是什么神奇的卡点,我表示很迷.jpg
17     {
18         int x[maxn],y[maxn];
19         int ans=0;
20         for(int i=k+1;i<=2*k;i++)//枚举y
21         {
22             int m=(k*i)/(i-k);
23             if((k*i)%(i-k)==0&&m>=i)
24                 {
25                     x[ans]=m,y[ans]=i;
26                     ans++;
27                 }
28         }
29         printf("%d\n",ans);
30         for(int i=0;i<ans;i++)
31             printf("1/%d = 1/%d + 1/%d\n",k,x[i],y[i]);
32         }
33     return 0;
34 }

思路:

Cuz: x>=y 则1/x<=1/y 则1/k=1/x+1/y<=2/y

So: y<=2k && y>k,x>=y(由题目得)

x=(k*y)/(y-k),如果满足(k*y)%(y-k)则存在这样的正整数x。

posted @ 2019-05-18 19:01  zuiaimiusi  阅读(121)  评论(0编辑  收藏  举报