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
题目类型:暴力枚举
算法分析:直接枚举出y,然后再计算出x是否满足要求,由x >= y,则可得1/x <= 1/y,代入表达式可得y <= 2k,所以枚举[1,2k]内的值作为y,然后生成x并判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
/************************************************** filename :e.cpp author :maksyuki created time :2018/6/11 11:06:29 last modified :2018/6/11 11:14:36 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; typedef pair<long long, long long> PLL; LL gcd(LL a, LL b) { return (!b)? a : gcd(b, a % b); } int main() { #ifdef LOCAL CFF; //CFO; #endif LL k; vector<PLL> ans; while(cin >> k) { ans.clear(); for(LL y = 1; y <= 2 * k; y++) { LL ta = gcd(y, k); LL tb = y * k / ta; LL tmpa = tb / k - tb / y; LL tmpb = gcd(tmpa, tb); tmpa /= tmpb; tb /= tmpb; if(tmpa == 1 && tb >= y) ans.emplace_back(PLL(y, tb)); } int alen = ans.size(); cout << alen << endl; for(int i = 0; i < alen; i++) cout << "1/" << k << " = " << "1/" << ans[i].second << " + " << "1/" << ans[i].first << endl; } return 0; } |
- « 上一篇:uva11059
- uva524:下一篇 »