F - 等式(1/x + 1/y = 1/n)
链接:https://www.nowcoder.com/acm/contest/90/F
来源:牛客网
题目描述
给定n,求1/x + 1/y = 1/n (x<=y)的解数。(x、y、n均为正整数)
输入描述:
在第一行输入一个正整数T。
接下来有T行,每行输入一个正整数n,请求出符合该方程要求的解数。
(1<=n<=1e9)
输出描述:
输出符合该方程要求的解数。
示例1
输入
3 1 20180101 1000000000
输出
1 5 181
思路:
1/x + 1/y = 1/n
-> yn + xn - xy = 0
-> yn + xn - xy -n^2 +n^2 = 0
-> n^2 = (n - x) * ( n - y)
即:求n^2的约数组合数,n^2的质因数与n的质因数相同,个数为n的两倍
代码:
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define is_lower(c) (c >= 'a' && c <= 'z') #define is_upper(c) (c >= 'A' && c <= 'Z') #define is_alpha(c) (is_lower(c) || is_upper(c)) #define is_digit(c) (c >= '0' && c <= '9') #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define PI acos(-1) #define IO \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define For(i, a, b) for (int i = a; i <= b; i++) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; const ll inf = 0x3f3f3f3f; const double EPS = 1e-10; const ll inf_ll = (ll)1e18; const ll maxn = 100005LL; const ll mod = 2012LL; const int N = 100000 + 5; int main() { int T,n; cin>>T; while(T--) { cin>>n; int ans[1000]={0}; int len = 1; for(int i = 2; i * i <= n; i++) { if(n % i == 0) { while(n % i == 0) { ans[len]++; n /= i; } len++; } } if(n != 1) ans[len]++; ll res = 1; for(int i = 1; i <= len ; i++) res *= ans[i] * 2 + 1; res = (res + 1) / 2; cout << res << endl; } }
宝剑锋从磨砺出 梅花香自苦寒来