Loading [MathJax]/jax/output/CommonHTML/jax.js

Peter's codebook A blog full of codes

POJ 1845 -- Sumdiv

質因數分解 + 模運算 + 等比級數

解法

先求出題目中 A 的質因數分解, 每個 A 的因數和可以用以下公式求得:

ai,biAQFabi+1i1ai1

其中, ai,bi 分別代表 A 的質因數、對應的次方,要算出 裡面的值,需要在取模底下做運算,所以需要模反元素、模底下的快速冪。

但是這種運算,在某些情況下,取模計算 abi+1i 有可能輸出 1 (非法),

這種情況,不可以直接像上面計算等比級數,就必須遞迴解等比級數。

計算方式在程式碼中。 (summ 函式)

傳送門

POJ1845

程式碼

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdio>
#include <assert.h>
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
const int M = 9901;

map<int,int> pd(int n) {
    map<int,int> r;
    for(int i=2; i*i<=n; ++i) {
        while(n%i==0) {
            ++r[i];
            n/=i;
        }
    }
    if(n!=1) r[n]=1;
    return r;
}

int fast_pow(int x, long long int n) {
    int res = 1%M;
    int base = x%M;
    n %= (long long int)(M-1);
    while(n>0L) {
        if (n&1L) {
            res = res * base % M;
        }
        base = base * base % M;
        n >>= 1L;
    }
    return res%M;
}

inline int inv_M(int a) {
    return fast_pow(a, M-2);
}

int summ(int a, int n) {
    if (n==0) return 1L;
    if (n%2L) { // 奇數
        return summ(a, n>>1L) * (1 + fast_pow(a, n/2+1)) % M;
    } 
    return (summ(a, (n>>1L)-1L) * (1 + fast_pow(a, n/2+1)) %M + fast_pow(a, n/2)) % M;
}

inline int cal(const int &a, const int &b, const int &n) {
    long long int p = (long long int)n * (long long int)b;
    int ar = fast_pow(a, p+1L);
    if (ar==1) return summ(a, p); // fails
    return ((ar - 1 + M)%M) * inv_M((a-1+M)%M) %M;
}

inline int solv(int A, int B) {
    int res=1;
    if (A==0) return 0;
    if (B==0) return 1;
    //A%=M;
    map<int, int> fac(pd(A));
    for (map<int,int>::iterator v=fac.begin(); v!=fac.end(); ++v) {
        //cout << v->first << ':' << v->second << endl;
        res = res * cal(v->first%M, v->second, B) % M;
    }

    return res;
}

int main(void) {
    int A, B;
    while(scanf("%d%d", &A, &B)==2) {
        printf("%d\n", solv(A,B));
    }
    return 0;
}