P5495
作者:互联网
Dirichlet 前缀和
复健一下子。。。
直接暴力枚举因数是 \(O(n \ln n)\) 的,这里卡的很紧,基本过不掉。
我们类似埃氏筛的方法来做前缀和即可,时间复杂度是 \(O(n\ln \ln n)\) 的,就能过了。
代码:
#include<iostream>
#include<cstdio>
#define ll long long
#define uint unsigned int
using namespace std;
namespace Ehnaev{
inline uint read() {
ll ret=0,f=1;char ch=getchar();
while(ch<48||ch>57) {if(ch==45) f=-f;ch=getchar();}
while(ch>=48&&ch<=57) {ret=(ret<<3)+(ret<<1)+ch-48;ch=getchar();}
return ret*f;
}
inline void write(uint x) {
static char buf[22];static ll len=-1;
if(x>=0) {do{buf[++len]=x%10+48;x/=10;}while(x);}
else {putchar(45);do{buf[++len]=-(x%10)+48;x/=10;}while(x);}
while(len>=0) putchar(buf[len--]);
}
}using Ehnaev::read;using Ehnaev::write;
const ll N=2e7;
uint seed,n;
uint a[N+5],b[N+5];
inline uint getnext(){
seed^=seed<<13;
seed^=seed>>17;
seed^=seed<<5;
return seed;
}
bool f[N+5];
ll cnt;
ll prime[N+5];
inline void Init() {
f[1]=1;
for(ll i=2;i<=n;i++) {
if(!f[i]) {prime[++cnt]=i;}
for(ll j=1;j<=cnt&&prime[j]*i<=n;j++) {
f[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
}
int main() {
n=read();seed=read();
for(ll i=1;i<=n;i++) {
a[i]=b[i]=getnext();
}
Init();
for(ll i=1;i<=cnt;i++) {
for(ll j=1;j*prime[i]<=n;j++) {
b[j*prime[i]]+=b[j];
}
}
uint ans=0;
for(ll i=1;i<=n;i++) {ans^=b[i];}
write(ans);
return 0;
}
标签:10,ch,P5495,len,while,seed,uint 来源: https://www.cnblogs.com/Apolynth/p/16460302.html