LG3768 simple math problems

P3768 simple math problems

Title Description

Enter an integer n and an integer p, you need to find $ (\ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ n ijgcd (i, j)) ~ mod ~ p $, where gcd (a , b) denotes the greatest common divisor of a and b.

Input and output formats

Input formats:

Row of two integers p, n.

Output formats:

A line integer $ (\ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ n ijgcd (i, j)) ~ mod ~ p $.

Sample input and output

Input Sample # 1: Copy
998244353 2000
Output Sample # 1: Copy
883968974

Explanation

For 20% of the data, $ n \ leq 1000 $.

For 30% of the data, $ n \ leq 5000 $.

For 60% of the data, $ n \ leq 10 ^ 6 $, time 1s.

For the other 20% of the data, $ n \ leq 10 ^ 9 $, time 3s.

For the last 20% of the data, $ n \ leq 10 ^ {10} $, time 6s.

To 100% of the data, $ 5 \ times 10 ^ 8 \ leq p \ leq 1.1 \ times 10 ^ 9 $ and p is a prime number.

answer

First push a wave equation.

\[ \sum_{i=1}^n\sum_{j=1}^nij\gcd(i,j)\\ =\sum_{i=1}^n\sum_{j=1}^nij \sum_{d|i\wedge d|j}\varphi(d)\\ =\sum_{d=1}^n\varphi(d)\sum_{d|i}\sum_{d|j}ij\\ =\sum_{d=1}^n\varphi(d)d^2(\sum_{i=1}^{\lfloor\frac nd\rfloor}i)^2\\ =\sum_{d=1}^n\varphi(d)d^2S(\lfloor\frac nd\rfloor,3) \]

Wherein \ (S \) represents a natural number and power. So long as the determined \ (f (n) = \ varphi (n) n ^ 2 \) prefix and \ (F. (N-) = \ sum_ = {I}. 1 ^ NF (I) \) , this formula integer block can do.

Consider \ (f (n) = \ varphi (n) n ^ 2 \) its form, \ (f = \ varphi \ cdot the above mentioned id ^ 2 \) , in line with the form Du teach sieve process. So configured \ (G (n-) = n-^ 2 \) , then
\ [(f * g) ( n) = \ sum_ {d | n} \ varphi (d) d ^ 2 (\ frac nd) ^ 2 = n ^ 3 \]
and \ (g, f * g \ ) prefix and is \ (S (the n-, 2), S (the n-, 3) \) , so this problem will be solved. The last column at summation:
\ [F. (N-) = \ sum_. 1} ^ {n-I = (F * G) (I) - \ sum_ {2} = I ^ ng (I) F. (\ Lfloor \ FRAC ni \ rfloor) \\ = \ sum_
{i = 1} ^ ni ^ 3- \ sum_ {i = 2} ^ ni ^ 2F (\ lfloor \ frac ni \ rfloor) \] time complexity \ (O (n ^ \ FRAC 23 is n-^ + \ 12 is FRAC) \) . Saying that the problem is really water data, plus I have written minus 60 points.

#include<bits/stdc++.h>
#define il inline
#define co const
template<class T>T read(){
    T data=0,w=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
    for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
    return data*w;
}
template<class T>il T read(T&x) {return x=read<T>();}
typedef long long LL;
using namespace std;

co int N=4641589;
int mod,i6;
int pri[N],tot,phi[N];
int add(int a,int b){
    return (a+=b)>=mod?a-mod:a;
}
int mul(int a,int b){
    return (LL)a*b%mod;
}
int fpow(int a,int b){
    int ans=1;
    for(;b;b>>=1,a=mul(a,a))
        if(b&1) ans=mul(ans,a);
    return ans;
}
void init(){
    i6=fpow(6,mod-2);
    pri[1]=phi[1]=1;
    for(int i=2;i<N;++i){
        if(!pri[i]) pri[++tot]=i,phi[i]=i-1;
        for(int j=1;j<=tot&&i*pri[j]<N;++j){
            pri[i*pri[j]]=1,phi[i*pri[j]]=phi[i]*phi[pri[j]];
            if(i%pri[j]==0){
                phi[i*pri[j]]=phi[i]*pri[j];
                break;
            }
        }
    }
    for(int i=2;i<N;++i) phi[i]=add(phi[i-1],mul(phi[i],mul(i,i)));
}
int sum_s2(int n){
    return mul(i6,mul(n,mul(n+1,2*n+1)));
}
int sum_s3(int n){
    return n&1?mul(mul(n,(n+1)/2),mul(n,(n+1)/2)):mul(mul(n/2,n+1),mul(n/2,n+1));
}
map<LL,int> sf;
int sum_f(LL n){
    if(n<N) return phi[n];
    if(sf.count(n)) return sf[n];
    int ans=sum_s3(n%mod);
    for(LL l=2,r;l<=n;l=r+1){
        r=n/(n/l);
        ans=add(ans,mod-mul(add(sum_s2(r%mod),mod-sum_s2((l-1)%mod)),sum_f(n/l)));
    }
    return sf[n]=ans;
}
int solve(LL n){
    int ans=0;
    for(LL l=1,r;l<=n;l=r+1){
        r=n/(n/l);
        ans=add(ans,mul(add(sum_f(r),mod-sum_f(l-1)),sum_s3(n/l%mod)));
    }
    return ans;
}
int main(){
    cerr<<(sizeof(pri)+sizeof(phi))/1024.0/1024<<endl;
    read(mod);
    init();
    printf("%d\n",solve(read<LL>()));
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/11104227.html