标签:style os io for div sp amp on size
题意:
求A^B的所有约数之和。
题解:
A = P1^a1 * P2^a2 * ... * Pn^an.
A^B的所有约数之和为:
sum = [1+p1+p1^2+...+p1^(a1*B)] * [1+p2+p2^2+...+p2^(a2*B)] *...* [1+pn+pn^2+...+pn^(an*B)].
用递归二分求等比数列1+pi+pi^2+pi^3+...+pi^n:
(1)若n为奇数,一共有偶数项,则:
1 + p + p^2 + p^3 +...+ p^n
= (1+p^(n/2+1)) + p * (1+p^(n/2+1)) +...+ p^(n/2) * (1+p^(n/2+1))
= (1 + p + p^2 +...+ p^(n/2)) * (1 + p^(n/2+1))
上式红色加粗的前半部分恰好就是原式的一半,那么只需要不断递归二分求和就可以了,后半部分为幂次式,将在下面第4点讲述计算方法。
(2)若n为偶数,一共有奇数项,则:
1 + p + p^2 + p^3 +...+ p^n
= (1+p^(n/2+1)) + p * (1+p^(n/2+1)) +...+ p^(n/2-1) * (1+p^(n/2+1)) + p^(n/2)
= (1 + p + p^2 +...+ p^(n/2-1)) * (1+p^(n/2+1)) + p^(n/2);
上式红色加粗的前半部分恰好就是原式的一半,依然递归求解
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 20000; const int mod = 9901; int p[maxn], k[maxn], cnt = 0; LL pow(LL a, LL b) { LL res = 1; while(b) { if(b&1) res = res*a % mod; a = a*a % mod; b >>= 1; } return res; } void factor(int n) { cnt = 0; int m = (int)sqrt(n + 0.5); for(int i=2; i<=m; i+=2) { if(!(n%i)) { p[cnt] = i; k[cnt] = 0; while(!(n%i)) { n /= i; k[cnt]++; } cnt++; } if(i==2) i--; } if(n>1) { p[cnt] = n; k[cnt] = 1; cnt++; } } LL sum(LL p, LL n) { if(n==0) return 1; if(n%2) return (sum(p,n/2)*(1+pow(p,n/2+1))) % mod; else return (sum(p,n/2-1)*(1+pow(p,n/2+1))+pow(p,n/2)) % mod; } int main() { int a, b; scanf("%d%d", &a, &b); factor(a); LL ans = 1; for(int i=0; i<cnt; ++i) { ans = (ans*(sum(p[i],k[i]*b)%mod))%mod; } printf("%d\n", ans); return 0; }
标签:style os io for div sp amp on size
原文地址:http://blog.csdn.net/yew1eb/article/details/38902817