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
| #include<bits/stdc++.h>
using namespace std;
template<typename T>void read(T&x){x=0;int fl=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') fl=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}x*=fl;} template<typename T,typename...Args>inline void read(T&t,Args&...args){read(t);read(args...);}
typedef long long LL; typedef vector<int> vi; typedef pair<int, int> pii; #define mp(x, y) make_pair(x, y) #define pb(x) emplace_back(x)
const int N = 5000005; LL p, inv2, inv6;
int phi[N], pList[N], pID; bool notP[N]; void Euler(int n){ notP[0] = notP[1] = 1; phi[1] = 1; for(int i = 1; i <= n; i++){ if(notP[i] == 0){ pList[++pID] = i; phi[i] = (i - 1) % p; } for(int j = 1; j <= pID; j++){ if(1ll * i * pList[j] > n) break; notP[i * pList[j]] = 1; if(i % pList[j] == 0){ phi[i * pList[j]] = 1ll * phi[i] * pList[j] % p; break; } else phi[i * pList[j]] = 1ll * phi[i] * (pList[j] - 1) % p; } } }
inline LL fpow(LL bs, LL idx, LL mod){ LL res = 1; bs %= mod; while(idx){ if(idx & 1) (res *= bs) %= mod; (bs *= bs) %= mod; idx >>= 1; } return res; }
unordered_map<LL, LL> s; LL pres[N]; LL apiadu(LL n){ if(n <= 5000000) return pres[n]; if(s.find(n) != s.end()) return s[n]; LL res = fpow(n%p*(n+1)%p*inv2%p, 2, p); for(LL l = 2, r; l <= n; l = r + 1){ r = n / (n / l); LL rs = r%p*(r+1)%p*(2*r%p+1)%p*inv6%p; LL ls = (l-1)%p*l%p*(2*l%p-1)%p*inv6%p; res -= (rs - ls) * apiadu(n / l) % p; ((res %= p) += p) %= p; } return s[n] = res; }
LL n; inline LL solve(){ LL res = 0; for(LL l = 1, r; l <= n; l = r + 1){ r = n / (n / l); res += fpow((n/l)%p*(n/l+1)%p, 2, p) * (apiadu(r) - apiadu(l-1)) % p; ((res %= p) += p) %= p; } return res * fpow(4, p-2, p) % p; }
int main(){ read(p, n); inv2 = fpow(2, p-2, p), inv6 = fpow(6, p-2, p); Euler(5000000); for(int i = 1; i <= 5000000; i++) pres[i] = (pres[i-1] + 1ll * i * i % p * phi[i] % p) % p; printf("%lld\n", solve()); return 0; }
|