QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#406632 | #8543. Periodic Sequence | xiaolang | TL | 2ms | 9880kb | C++14 | 2.4kb | 2024-05-07 15:29:27 | 2024-05-07 15:29:33 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+5;
int n,MOD;
int cc[N];
int anss[N];
int tar[N];
int inv[N];
int qpow(int x,int y,int mod){
if(y==0)return 1%mod;
if(y==1)return x%mod;
int kp=qpow(x,y/2,mod);
if(y&1)return kp*kp%mod*x%mod;
return kp*kp%mod;
}
int cnt=0;
namespace Polynomial{
int cc[N];
void mul(vector<pair<int,int> > a,vector<pair<int,int> > b){
int lena=a.size();
int lenb=b.size();
for(int i=0;i<=n;i++){
cc[i]=0;
}
for(int i=0;i<lena;i++){
for(int j=0;j<lenb;j++){
anss[a[i].first+b[j].first]=(anss[a[i].first+b[j].first]+a[i].second*b[j].second)%MOD;
}
}
}
vector<pair<int,int> > mulinv(vector<pair<int,int> > x){
int xlen=x.size();
for(int i=0;i<=n;i++){
cc[i]=0;
tar[i]=0;
}
tar[0]=1;
vector<pair<int,int> >ans;
for(int i=0;i<=n;i++){
if(cc[i]==tar[i])continue;
else{
cnt++;
int nowc=(cc[i]-tar[i]+MOD)%MOD;
ans.push_back(make_pair(i,nowc));
for(int j=0;j<xlen;j++){
cc[i+x[j].first]=(cc[i+x[j].first]+nowc*x[j].second)%MOD;
}
}
}
return ans;
}
vector<int> div(vector<int> c){/* /(2x-1) */
vector<int>ans;
ans.clear();
int len=c.size();
for(int i=0;i<len-1;i++){
int t=(MOD-c[i])%MOD;
ans.push_back(t);
c[i+1]=(c[i+1]-2*t+MOD+MOD)%MOD;
}
return ans;
}
};
using namespace Polynomial;
vector<pair<int,int> > fz;
vector<pair<int,int> > fm;
struct Addition{
int t,xmi,c;
}add[N];
bool cmp(Addition x,Addition y){
return x.t>y.t;
}
int maxdep=0;
int addlen=0;
signed main(){
//freopen("1.txt","r",stdin);
//freopen("1.out","w",stdout);
int tim1=clock();
scanf("%lld%lld",&n,&MOD);
int B=sqrt(n);
for(int i=1;i<=B;i++){
fz.clear();
fz.push_back(make_pair(i,MOD-1));
fm.clear();
fm.push_back(make_pair(0,MOD-1));
fm.push_back(make_pair(1,2));
fm.push_back(make_pair(i+1,MOD-1));
mul(fz,mulinv(fm));
}
for(int i=B+1;i<=n;i++){
for(int j=1;j*i-1<=n;j++){
add[++addlen]=(Addition){j,j*i+j-1,-1};
maxdep=max(maxdep,j);
}
}
sort(add+1,add+addlen+1,cmp);
vector<int>ans(n+1000);
int nowadd=1;
for(int i=maxdep;i>=1;i--){
while(add[nowadd].t>=i){
ans[add[nowadd].xmi]=(ans[add[nowadd].xmi]+add[nowadd].c)%MOD;
nowadd++;
}
ans=div(ans);
}
int tot=0;
for(int i=1;i<=n;i++){
cout<<(ans[i]+anss[i])%MOD<<" ";
}
cout<<"\n";
int tim2=clock();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9880kb
input:
5 1000000007
output:
1 3 6 11 19
result:
ok 5 number(s): "1 3 6 11 19"
Test #2:
score: -100
Time Limit Exceeded
input:
200000 567894337