Color

http://poj.org/problem?id=2154

 

long long 超时, int 过了。。。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <algorithm>
 6 using namespace std;
 7 #define LL long long
 8 int p;
 9 int quickpow(int a, int b, int mod){
10     int temp = a % mod, res = 1;
11     while(b){
12         if(b & 1) res = res * temp % mod;
13         b >>= 1;
14         temp = temp * temp % mod;
15     }
16     return res;
17 }
18 const int maxn = 36010;
19 int pri[maxn];
20 int cnt;
21 void init(){
22     cnt = 0;
23     memset(pri, 0, sizeof(pri));
24     for(int i = 2; i < maxn; i++){
25         if(!pri[i]){
26             pri[cnt++] = i;
27             for(int j = i * i; j < maxn; j += i) pri[j] = 1;
28         }
29     }
30 }
31 int phi(int n){
32     int res = n;
33     for(int i = 0; pri[i] * pri[i] <= n; i++) {
34         if(n % pri[i] == 0){
35             res = res - res / pri[i];
36             while(n % pri[i] == 0) n /= pri[i];
37         }
38     }
39     if(n > 1){
40         res = res - res / n;
41     }
42     return res % p;
43 }
44 int main(){
45     int c, n;
46     //freopen("in.txt", "r", stdin);
47     int t;
48     init();
49     scanf("%d", &t);
50     while(t--){
51         scanf("%d %d", &n, &p);
52         int ans = 0;
53         c = n;
54         for(int i = 1 ; i * i <= n; i++){
55             if(i * i == n) ans = (ans + quickpow(c, i - 1, p) * phi(i)) % p;
56             else if(n % i == 0){
57                 ans = (ans + quickpow(c, i - 1, p) * phi(n / i) + quickpow(c, n / i - 1, p) * phi(i)) % p;
58             }
59         }
60         printf("%d\n", ans);
61     }
62 }
View Code

 

posted @ 2017-12-02 20:48  yijiull  阅读(122)  评论(0编辑  收藏  举报