一定要先%mmh学长。
做题前没有%mmh学长,一直WA。
例①:Libre OJ #6053. 简单的函数:
定义函数
(1) f ( 1 ) = 1。
(2) f ( pc ) = p ⊕ c,p为质数,⊕为异或
(3) f ( a b ) = f ( a ) f ( b ) a与b互质。
求前n项和ans。 输出ans%(1e9+7)。
其中n小于等于1e10。
题解:。。。min_25 筛
因为有个地方相乘爆long long,忘了先取模,调了一晚上。
以后还是要先%mmh学长。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#define ll long long
#define llu unsigned ll
#define iu unsigned int
using namespace std;
const int maxn=200100;
const int mod=1e9+7;
ll p[maxn],id1[maxn],id2[maxn];
ll g[maxn],h[maxn],sum[maxn],wi[maxn];
int cnt;
bool ha[maxn];
ll n,inv2,sq;
ll mypow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void Prime(ll n)
{
ha[1]=true;
for(int i=2;i<=n;i++)
{
if(!ha[i])
{
p[++cnt]=i;
sum[cnt]=(sum[cnt-1]+i)%mod;
}
for(int j=1;j<=cnt&&p[j]*i<=n;j++)
{
ha[i*p[j]]=true;
if(i%p[j]==0) break;
}
}
}
int getid(ll m)
{
if(m<=sq) return id1[m];
else return id2[n/m];
}
ll s(ll n,ll j)
{
if(n<=1||p[j]>n) return 0;
int k=getid(n);
ll res=(((g[k]-sum[j-1])-(h[k]-(j-1)))%mod+mod)%mod;
if(j==1) res=(res+2)%mod;
for(int i=j;i<=cnt&&p[i]*p[i]<=n;i++)
{
ll p1=p[i],p2=p[i]*p[i];
for(int e=1;p2<=n;e++,p1=p2,p2=p2*p[i])
{
res=(res+s(n/p1,i+1)*(p[i]^e)%mod+(p[i]^(e+1)))%mod;
}
}
return res;
}
int main(void)
{
inv2=mypow(2,mod-2);
scanf("%lld",&n);
sq=sqrt(n*1.0);
Prime(sq);
int tot=0;
for(ll l=1,r;l<=n;l=r+1)
{
r=n/(n/l);
wi[++tot]=n/l;
if(wi[tot]<=sq) id1[wi[tot]]=tot;
else id2[n/wi[tot]]=tot;
g[tot]=(((wi[tot]+2)%mod)*((wi[tot]-1)%mod))%mod*inv2%mod;
h[tot]=(wi[tot]-1)%mod;
}
for(int j=1;j<=cnt;j++)
{
for(int i=1;i<=tot&&p[j]*p[j]<=wi[i];i++)
{
int k=getid(wi[i]/p[j]);
g[i]=((g[i]-p[j]*(g[k]-sum[j-1])%mod)%mod+mod)%mod;
h[i]=((h[i]-1ll*(h[k]-(j-1))%mod)%mod+mod)%mod;
}
}
ll ans=s(n,1);
printf("%lld\n",(ans+1)%mod);
return 0;
}
例②:P3912 素数个数:
题目描述
求1,2,⋯,N 中素数的个数。
输入格式
1 个整数N。
输出格式
1 个整数,表示素数的个数。
输入输出样例
输入 #1 复制
10
输出 #1 复制
4
说明/提示
1≤N≤1e8 。
min_25 稍微筛一下就好了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#define ll long long
#define llu unsigned ll
#define iu unsigned int
using namespace std;
const int maxn=200100;
const int mod=1e9+7;
ll p[maxn],id1[maxn],id2[maxn];
ll g[maxn],wi[maxn];
int cnt;
bool ha[maxn];
ll n,sq;
void Prime(ll n)
{
ha[1]=true;
for(int i=2;i<=n;i++)
{
if(!ha[i])
p[++cnt]=i;
for(int j=1;j<=cnt&&p[j]*i<=n;j++)
{
ha[i*p[j]]=true;
if(i%p[j]==0) break;
}
}
}
int getid(ll m)
{
if(m<=sq) return id1[m];
else return id2[n/m];
}
int main(void)
{
scanf("%lld",&n);
sq=sqrt(n*1.0);
Prime(sq);
int tot=0;
for(ll l=1,r;l<=n;l=r+1)
{
r=n/(n/l);
wi[++tot]=n/l;
if(wi[tot]<=sq) id1[wi[tot]]=tot;
else id2[n/wi[tot]]=tot;
g[tot]=wi[tot]-1;
}
for(int j=1;j<=cnt;j++)
{
for(int i=1;i<=tot&&p[j]*p[j]<=wi[i];i++)
{
int k=getid(wi[i]/p[j]);
g[i]=g[i]-1ll*(g[k]-(j-1));
}
}
printf("%lld\n",g[1]);//tot=1时,wi[1]=N,是前N项和。
return 0;
}
以后一定要先%mmh学长,要不然程序要调一整晚上。。。