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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include <iostream> #include <vector>
using namespace std;
typedef __int128_t int128; typedef long long ll;
ll power(ll base, ll exp, ll mod) { ll res = 1; base %= mod; while (exp > 0) { if (exp % 2 == 1) res = (int128)res * base % mod; base = (int128)base * base % mod; exp /= 2; } return res; }
bool is_prime(ll n) { if (n < 2) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0) return false; ll d = n - 1; int s = 0; while (d % 2 == 0) { d /= 2; s++; } static const vector<ll> bases = {2, 3, 5, 7, 11, 13, 17, 19, 23}; for (ll a : bases) { if (n <= a) break; ll x = power(a, d, n); if (x == 1 || x == n - 1) continue; bool composite = true; for (int r = 1; r < s; r++) { x = (int128)x * x % n; if (x == n - 1) { composite = false; break; } } if (composite) return false; } return true; }
ll solve_Sk(ll N, int k, ll p) { if (N <= k + 3) { ll current_f = 0, current_S = 0; for (int i = 1; i <= N; ++i) { current_f = (current_f + power(i, k, p)) % p; current_S = (current_S + current_f) % p; } return current_S; }
vector<ll> y(k + 4); ll current_f = 0; for (int i = 1; i <= k + 3; ++i) { current_f = (current_f + power(i, k, p)) % p; y[i] = (y[i - 1] + current_f) % p; }
ll m = k + 3; N %= p; vector<ll> pre(m + 2, 1), suf(m + 2, 1); for (int i = 1; i <= m; ++i) pre[i] = (int128)pre[i - 1] * (N - i + p) % p; for (int i = m; i >= 1; --i) suf[i] = (int128)suf[i + 1] * (N - i + p) % p;
vector<ll> fact(m + 1, 1), invFact(m + 1, 1); for (int i = 1; i <= m; ++i) fact[i] = (int128)fact[i - 1] * i % p; invFact[m] = power(fact[m], p - 2, p); for (int i = m - 1; i >= 0; --i) invFact[i] = (int128)invFact[i + 1] * (i + 1) % p;
ll ans = 0; for (int i = 1; i <= m; ++i) { ll num = (int128)pre[i - 1] * suf[i + 1] % p; ll den = (int128)invFact[i - 1] * invFact[m - i] % p; ll term = (int128)y[i] * num % p * den % p; if ((m - i) % 2 == 1) ans = (ans - term + p) % p; else ans = (ans + term) % p; } return ans; }
int main() { ll start = 2000000000; ll end = 2000000000 + 2000; ll N = 1000000000000LL; int k = 10000; ll total_sum = 0;
for (ll p = start; p <= end; ++p) { if (is_prime(p)) { total_sum += solve_Sk(N, k, p); cout << "P: " << p << " calculated." << endl; } }
cout << "Final Result: " << total_sum << endl; return 0; }
|