题意: 给你 N 和 K,问有多少个数对满足 gcd(N-A, N) * gcd(N - B, N) = N^K 分析: 由于 gcd(a, N) <= N,于是 K>2 都是无解,K=2 只有一个解 A=B=N,只要考虑 K = 1 的情况就好了 其实上式和这个是等价的 gcd(A, N) * gcd(B, N) = N^K,我们枚举 gcd(A, N) = g,那么gcd(B, N) = N / g。问题转化为统计满足 gcd(A, N) = g 的 A 的个数。这个答案就是 ɸ(N/g) 只要枚举 N 的 约数就可以了。答案是 Σɸ(N/g)*ɸ(g) g | N计算 ɸ 可以递归,也可以直接暴力计算,两个都可以。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 100005 #define maxm 40005 #define eps 1e-10 #define mod 1000000007 #define INF 999999999 #define lowbit(x) (x&(-x)) #define mp mark_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R typedef long long LL; //typedef int LL; using namespace std; LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);} // head LL phi(LL x) { LL ans = x; for(LL i = 2; i * i <= x; i++) if(x % i == 0) { ans -= ans/i; while(x % i == 0) x /= i; } if(x > 1) ans -= ans/x; return ans; } int main(void) { LL n, k; while(scanf("%I64d%I64d", &n, &k)!=EOF) { if(k > 2) { if(n == 1) printf("1\n"); else printf("0\n"); } else if(k == 2) printf("1\n"); else { LL ans = 0; for(LL i = 1; i * i <= n; i++) if(n % i == 0) { if(i * i != n) ans = (ans + phi(i) * phi(n/i) * 2) % mod; else ans = (ans + phi(i) * phi(i)) % mod; } printf("%I64d\n", ans); } } return 0; }
【欧拉函数】 HDOJ 4983 Goffi and GCD
最新推荐文章于 2021-02-04 00:16:09 发布