Poj 1845 Sumdiv, qualitative factor decomposition
Question:
Calculate the sum of all the approx. s of A ^ B.
Question:
A = P1 ^ a1 * P2 ^ a2 *... * Pn ^.
The sum of all approx. A ^ B:
Sum = [1 + p1 + p1 ^ 2 +... + p1 ^ (a1 * B)] * [1 + p2 + p2 ^ 2 +... + p2 ^ (a2 * B)] *... * [1 + pn ^ 2 +... + pn ^ (an * B)].
Use recursive binary algorithms to obtain the proportional sequence 1 + pi ^ 2 + pi ^ 3 +... + pi ^ n:
(1) If n is an odd number and there is a total of even numbers, then:
1 + 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 ^ 2 +... + p ^ (n/2) * (1 + p ^ (n/2 + 1 ))
The first half of the above formula is exactly half of the original formula, so you only need to recursive the binary summation continuously, and the second half is the power type, the calculation method is described in the following 4th points.
(2) If n is an even number and there is a total of odd values, then:
1 + 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 ^ 2 +... + p ^ (n/2-1) * (1 + p ^ (n/2 + 1) + p ^ (n/2 );
The first half of the above-formula red bold is exactly half of the original formula.
#include
#include
#include
#include
#includeusing 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