板子记录。
1 #include<bits/stdc++.h> 2 #define ll long long 3 #define scan(i) scanf("%d",&i) 4 #define scand(i) scanf("%lf",&i) 5 #define scanl(i) scanf("%lld",&i) 6 #define f(i,a,b) for(int i=a;i<=b;i++) 7 #define pb(i) push_back(i) 8 #define ppb pop_back() 9 #define pf printf 10 #define dbg(args...) cout<<#args<<" : "<<args<<endl; 11 #define input freopen("in.txt","r",stdin) 12 #define output freopen("out.txt","w",stdout) 13 #define io ios::sync_with_stdio(0) 14 15 #define LINT long long 16 using namespace std; 17 18 inline LINT V2IDX(LINT v, LINT N, LINT Ndr, LINT nv) { 19 return v >= Ndr ? (N/v - 1) : (nv - v); 20 } 21 22 LINT primesum(LINT N) { 23 LINT *S; 24 LINT *V; 25 26 LINT r = (LINT)sqrt(N); 27 LINT Ndr = N/r; 28 29 // assert(r*r <= N and (r+1)*(r+1) > N); 30 31 LINT nv = r + Ndr - 1; 32 33 V = new LINT[nv]; 34 S = new LINT[nv]; 35 36 for (LINT i=0; i<r; i++) { 37 V[i] = N/(i+1); 38 } 39 for (LINT i=r; i<nv; i++) { 40 V[i] = V[i-1] - 1; 41 } 42 43 for (LINT i=0; i<nv; i++) { 44 S[i] = V[i] * (V[i] + 1) / 2 - 1; 45 } 46 47 for (LINT p=2; p<=r; p++) { 48 if (S[nv-p] > S[nv-p+1]) { 49 LINT sp = S[nv-p+1]; 50 LINT p2 = p*p; 51 for (LINT i=0; i<nv; i++) { 52 if (V[i] >= p2) { 53 S[i] -= p * (S[V2IDX(V[i]/p, N, Ndr, nv)] - sp); 54 } else { 55 break; 56 } 57 } 58 } 59 } 60 return S[0]; 61 } 62 63 ostream& operator<<(ostream& os, __int128 t) { 64 if (t==0) return os << "0"; 65 if (t<0) { 66 os<<"-"; 67 t=-t; 68 } 69 int a[50],ai=0; 70 memset(a,0,sizeof a); 71 while (t!=0){ 72 a[ai++]=t%10; 73 t/=10; 74 } 75 for (int i=1;i<=ai;i++) os<<abs(a[ai-i]); 76 return os<<""; 77 } 78 79 int main() { 80 // LINT N = 1000000000; 81 // printf("%lld ", primesum(N)); 82 //cout<<primesum(5)<<endl; 83 //freopen("out.txt","r",stdin); 84 io; 85 int t; 86 ll mod,n; 87 scan(t); 88 f(kk,1,t){ 89 scanl(n); 90 scanl(mod); 91 if(n==1){ 92 puts("0"); 93 } 94 else if(n==2){ 95 puts("6"); 96 } 97 else{ 98 __int128 ans=__int128(n+2)*__int128(n+1)/2; 99 ans%=mod; 100 ans+=primesum(n+1); 101 ans-=5; 102 ans=ans%mod; 103 // cout<<ans<<endl; 104 ll x=ans; 105 pf("%lld ",x); 106 } 107 // ll ans2=ans%mod; 108 // pf("%lld ",ans); 109 } 110 }