P3768 简单的数学题 洛谷 (杜教筛)

在这里插入图片描述

#include <bits/stdc++.h>
#define ll long long
#define int long long 
using namespace std;
template <class cl>
void read(cl &x)
{
    x = 0;
    int f = 0;
    char ch;
    ch = getchar();
    while (!isdigit(ch))
    {
        f = f | (ch == '-'), ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x = f ? -x : x;
    return;
}
template <class cl>
void put(cl x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        put(x / 10);
    putchar(x % 10 + '0');
    return;
}
int mod;
int kpow(int a,int b)
{
   a=a%mod;
   int ans=1;
   while(b)
   {
       if(b&1)
       {
          ans=ans%mod*(a%mod)%mod;
       }
       b=b>>1;
       a=(a%mod)*(a%mod)%mod;
   }
   return ans;
}
int cnt=0;
// 害,搞了半天是把平方和和立方和用混了。
const int N=5e6+10;
const int nn=5e6;
int prime[N];
int phi[N];
bitset<N>g;
void fip()
{
    g[1]=g[0]=1;phi[1]=1;
    for(int i=2;i<=nn;i++)
    {
        if(g[i]==0)
        {
            prime[++cnt]=i;
            phi[i]=i-1;
        }
        for(int j=1;j<=cnt&&prime[j]<=nn/i;j++)
        {
            g[i*prime[j]]=1;
            if(i%prime[j]==0)
            {
                phi[i*prime[j]]=prime[j]*phi[i];
                break;
            }
            phi[i*prime[j]]=phi[i]*(prime[j]-1);
        }
    }
    for(int i=1;i<=nn;i++)
    {
        phi[i]=(phi[i-1]+i*i%mod*phi[i]%mod)%mod;
    }
}
int inv6;
unordered_map<int,int>mp;
int sum3(int x)//立方和公式
{
    x=x%mod;
    int tt=(x*(x+1)/2)%mod;
    return tt*tt%mod;
}
int sum2(int x)//平方和公式
{
    x=x%mod;
    return x*(x+1)%mod*(2*x+1)%mod*inv6%mod;
}
int du(int x)
{
   if(x<=nn) return phi[x];
   if(mp[x]) return mp[x];
   int res=sum3(x);
   int ree=0;
   for(int l=2,r;l<=x;l=r+1)
   {
       r=x/(x/l);
       ree=(ree+(sum2(r)-sum2(l-1)+mod)%mod*du(x/l)%mod)%mod;
   }
   res=(res-ree+mod)%mod;
   return mp[x]=res;
}
void to()
{
    int n;
    read(mod);
    read(n);
    inv6=kpow(6,mod-2);
    fip();
    int ans=0;
    for(int l=1,r;l<=n;l=r+1)
    {
        r=n/(n/l);
        ans=(ans+(du(r)-du(l-1)+mod)%mod*sum3(n/l)%mod)%mod;
    }
    put(ans);
    puts("");return ;
}
signed main()
{
    to();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值