hdu 1452 Happy 2004 找循环节
题目链接:
hdu 1452 Happy 2004
题意:求2004^X 约数之和S与29取模的结果。
分析:2004 = 2 * 2 * 3 * 167, 2004^x = 2^(2x) * 3^x * 167^x, 求约数之和S ,我们可以构造多项式:
S=(2^0+2^1+...+2^(2x)) * (3^0+3^1+...+3^x) * (167^0+167^1+...+167^x).
打印出X∈[1,100] 范围内的结果。找出循环节。周期为28。
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define fst first
#define snd second
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef __int64 LL;
//typedef long long LL;
typedef unsigned int uint;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int MAXN = 100000 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 29;
int X, S;
int v[] = {2, 3, 167};
int d[3];
int res[MOD + 5];
int main() {
#ifndef ONLINE_JUDGE
FIN;
// FOUT;
#endif // ONLINE_JUDGE
for(X = 1; X < MOD; X++) {
d[0] = (X << 1);
d[1] = d[2] = X;
S = 1;
int SUM;
SUM = 0;
for(int i = 0, a = 1; i <= d[0]; i++, a = (a * v[0]) % MOD) {
SUM = (SUM + a) % MOD;
}
S = (S * SUM) % MOD;
SUM = 0;
for(int i = 0, b = 1; i <= d[1]; i++, b = (b * v[1]) % MOD) {
SUM = (SUM + b) % MOD;
}
S = (S * SUM) % MOD;
SUM = 0;
for(int i = 0, c = 1; i <= d[2]; i++, c = (c * v[2]) % MOD) {
SUM = (SUM + c) % MOD;
}
S = (S * SUM) % MOD;
res[X] = S;
}
while(~scanf("%d", &X) && X) {
printf("%d\n", res[(X - 1) % 28 + 1]);
}
return 0;
}