题意:输入正整数k,找到所有的正整数 x≥y ,使得 1k=1x+1y 。(本段摘自《算法竞赛入门经典(第2版)》
分析:由等式和不等式可推出 y≤k ,则在2k范围内枚举y即可。
代码:
#include <fstream>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <sstream>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <vector>
using namespace std;
const int maxn = 2000 + 5;
int n, tmpx, tmpy, tmp;
vector< pair< int, int> > vec;
int gcd(int x, int y)
{
return (y == 0) ? x : gcd(y, x % y);
}
int main()
{
while (~scanf("%d", &n))
{
vec.clear();
for (int i = n + 1; i <= n + n; ++i)
{
tmpx = i - n;
tmpy = n * i;
tmp = gcd(tmpx, tmpy);
if (tmpx / tmp == 1)
vec.push_back(make_pair(tmpy / tmp, i));
}
printf("%d\n", (int)vec.size());
for (vector< pair< int, int> >::iterator it = vec.begin(); it != vec.end(); ++it)
printf("1/%d = 1/%d + 1/%d\n", n, it->first, it->second);
}
return 0;
}