链接:https://www.nowcoder.com/acm/contest/90/F 来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述
给定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 –> nx+ny=xy –> (n-x)*(n-y)=n*n
其实就是求 n * n <= n 的因子个数 不能直接求 数据太大
分解质因子降低时间复杂度
AC代码
#include <cstdio> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cmath> #include <climits> #include <ctime> #include <iostream> #include <algorithm> #include <deque> #include <vector> #include <queue> #include <string> #include <map> #include <stack> #include <set> #include <numeric> #include <sstream> #include <iomanip> #include <limits> #define CLR(a) memset(a, 0, sizeof(a)) #define pb push_back using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef pair<string, int> psi; typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327; const double E = exp(1); const double eps = 1e-6; const int INF = 0x3f3f3f3f; const int maxn = 1e5 + 5; const int MOD = 1e9 + 7; int main() { ll t; cin >> t; while (t--) { ll n; cin >> n; ll temp = n * n; ll ans = 1; for (ll i = 2; i * i <= temp; i++) { int cnt = 1; while (temp % i == 0) { temp /= i; cnt++; } ans *= cnt; } cout << ans / 2 + 1 << endl; } }转载于:https://www.cnblogs.com/Dup4/p/9433210.html
相关资源:JAVA上百实例源码以及开源项目