数论基本糙作:
gcd,快速幂,逆元,欧拉函数,分解因数balabala一通乱搞。
POJ1845 Sumdiv (数论:算数基本定理+数论基本操作)
题目:
Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29012 Accepted: 7127 Description Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of S by 9901). Input The only line contains the two natural numbers A and B, (0 <= A,B <= 50000000)separated by blanks. Output The only line of the output will contain S modulo 9901. Sample Input 2 3 Sample Output 15 Hint 2^3 = 8. The natural divisors of 8 are: 1,2,4,8. Their sum is 15. 15 modulo 9901 is 15 (that should be output).
代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const int MAX_N = 10000; const int MOD = 9901; ll fpow(ll a, ll p) { ll ans = 1; while (p) { if (p&1) ans = (ans*a) % MOD; a = (a*a) % MOD; p >>= 1; } return ans; } int prime[MAX_N+1]; void getPrime() { memset(prime, 0, sizeof prime); for (int i = 2; i <= MAX_N; i++) { if (!prime[i]) prime[++prime[0]] = i; for (int j = 1; j <= prime[0] && prime[j] <= MAX_N/i; j++) { prime[prime[j]*i] = 1; if (i%prime[j] == 0) break; } } } ll m; ll p[100], c[100]; ll getFactors(ll x) { m = 0; for (int i = 1; prime[i] <= x/prime[i]; i++) { if (x%prime[i] == 0) { p[m] = prime[i]; c[m] = 0; while (x%prime[i] == 0) { c[m]++; x /= prime[i]; } m++; } } if (x > 1) { p[m] = x; c[m] = 1; m++; } return m; } int main() { getPrime(); ll A, B; cin >> A >> B; getFactors(A); ll ans = 1; for (int i = 0; i < m; i++) { if (p[i] % MOD == 1) { ans = (B*c[i]+1)%MOD * ans % MOD; continue; } ll a = (fpow(p[i], B*c[i]+1)-1+MOD) % MOD; ll b = fpow(p[i]-1, MOD-2); ans = ans * a % MOD * b % MOD; } cout << ans << endl; return 0; }
POJ3696 The Luckiest Number (数论:欧拉定理+数论基本操作)
题目:
The Luckiest number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6829 Accepted: 1820 Description Chinese people think of ‘8‘ as the lucky digit. Bob also likes digit ‘8‘. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of only digit ‘8‘. Input The input consists of multiple test cases. Each test case contains exactly one line containing L(1 ≤ L ≤ 2,000,000,000). The last test case is followed by a line containing a zero. Output For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the length of Bob‘s luckiest number. If Bob can‘t construct his luckiest number, print a zero. Sample Input 8 11 16 0 Sample Output Case 1: 1 Case 2: 2 Case 3: 0
代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const int MAX_N = 10000; const int MOD = 9901; ll fpow(ll a, ll p) { ll ans = 1; while (p) { if (p&1) ans = (ans*a) % MOD; a = (a*a) % MOD; p >>= 1; } return ans; } int prime[MAX_N+1]; void getPrime() { memset(prime, 0, sizeof prime); for (int i = 2; i <= MAX_N; i++) { if (!prime[i]) prime[++prime[0]] = i; for (int j = 1; j <= prime[0] && prime[j] <= MAX_N/i; j++) { prime[prime[j]*i] = 1; if (i%prime[j] == 0) break; } } } ll m; ll p[100], c[100]; ll getFactors(ll x) { m = 0; for (int i = 1; prime[i] <= x/prime[i]; i++) { if (x%prime[i] == 0) { p[m] = prime[i]; c[m] = 0; while (x%prime[i] == 0) { c[m]++; x /= prime[i]; } m++; } } if (x > 1) { p[m] = x; c[m] = 1; m++; } return m; } int main() { getPrime(); ll A, B; cin >> A >> B; getFactors(A); ll ans = 1; for (int i = 0; i < m; i++) { if (p[i] % MOD == 1) { ans = (B*c[i]+1)%MOD * ans % MOD; continue; } ll a = (fpow(p[i], B*c[i]+1)-1+MOD) % MOD; ll b = fpow(p[i]-1, MOD-2); ans = ans * a % MOD * b % MOD; } cout << ans << endl; return 0; }
原文地址:https://www.cnblogs.com/Lubixiaosi-Zhaocao/p/9922504.html
时间: 2024-10-09 14:11:18