hdu 1452 数论

这题比较好,考查了数论的许多知识,包括一个数所有因子的和的形式,以及同余公式(除法的同余公式,即求同余下的逆元)等,网上好多讨论的,这里就不再啰嗦了,直接贴代码。

/*
* hdu1452/win.cpp
* Created on: 2011-10-24
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MOD = 29;
typedef long long LL;
int modular_exp(int a, int b, int c) {
LL res, temp;
res = 1 % c, temp = a % c;
while (b) {
if (b & 1) {
res = res * temp % c;
}
temp = temp * temp % c;
b >>= 1;
}
return (int) res;
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int x;
int firstpart, secondpart, thirdpart;
while (scanf("%d", &x) == 1 && x > 0) {
firstpart = modular_exp(2, 2 * x + 1, MOD) - 1;
secondpart = modular_exp(3, x + 1, MOD) - 1;
secondpart = (secondpart * 15) % MOD;
thirdpart = modular_exp(167, x + 1, MOD) - 1;
thirdpart = (thirdpart * 18) % MOD;
int ans = firstpart * secondpart * thirdpart;
ans %= MOD;
printf("%d\n", ans);
}
return 0;
}



原文地址:https://www.cnblogs.com/moonbay/p/2223048.html