POJ1458 Sumdiv

题目地址

题解

分治法求解等比数列和的一个运用。
每次把数列分成两半,就可以做到$O(logn)$了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
int f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
int a, b, sum = 1;
int poww(int d, int e){
int res = 1;
while(e){
if(e & 1)
res = (res * d) % 9901;
d = (d * d) % 9901;
e >>= 1;
}
return res;
}
int f(int p, int c){
if(c == 0) return 1;
if(c == 1) return (1 + p) % 9901;
if(c & 1)
return f(p, c >> 1) * (1 + poww(p, (c + 1) >> 1)) % 9901;
else
return ((f(p, c >> 1) + 9900) * (1 + poww(p, c >> 1)) + 1) % 9901;
}
void init(){
a = read(), b = read();
}
void solve(){
int t = a;
for(int i = 2; i * i <= a; ++i){
if(t % i == 0){
int cc = 0;
do{
t /= i, cc++;
}while(t % i == 0);
sum = sum * f(i % 9901, cc * b) % 9901;
}
if(t == 1) break;
}
if(t != 1) sum = sum * f(t % 9901, b) % 9901;
printf("%d\n", sum);
}
int main(){
init();
solve();
return 0;
}