QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#277910 | #7881. Computational Complexity | zhouhuanyi | RE | 0ms | 0kb | C++20 | 6.2kb | 2023-12-07 09:03:43 | 2023-12-07 09:03:43 |
answer
#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#define N 50
#define W 20000000000000
#define M 1000000
using namespace std;
long long read()
{
char c=0;
long long sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
struct reads
{
int num,type;
bool operator < (const reads &t)const
{
return num!=t.num?num<t.num:type<t.type;
}
};
struct node
{
reads num;
long long data;
};
node tong[M+1],tong2[M+1];
reads operator * (reads a,int b)
{
return (reads){a.num*b,!a.type};
}
int T,length,length2,lg[M+1];
long long A[N+1],B[N+1],F[7*N+1],G[7*N+1],mod;
map<reads,long long>dp;
map<long long,__int128>DSP;
map<long long,__int128>DSP2;
set<reads>s;
void bfs()
{
reads top;
s.insert((reads){1,0}),dp[(reads){1,0}]=1;
while (!s.empty())
{
top=*s.begin(),s.erase(top),tong[++length]=(node){top,dp[top]};
if (!top.type)
{
if ((top*2).num<=W) dp[top*2]+=dp[top],s.insert(top*2);
if ((top*3).num<=W) dp[top*3]+=dp[top],s.insert(top*3);
if ((top*5).num<=W) dp[top*5]+=dp[top],s.insert(top*5);
if ((top*7).num<=W) dp[top*7]+=dp[top],s.insert(top*7);
}
else
{
if ((top*2).num<=W) dp[top*2]+=dp[top],s.insert(top*2);
if ((top*3).num<=W) dp[top*3]+=dp[top],s.insert(top*3);
if ((top*4).num<=W) dp[top*4]+=dp[top],s.insert(top*4);
if ((top*5).num<=W) dp[top*5]+=dp[top],s.insert(top*5);
}
}
s.clear(),dp.clear(),s.insert((reads){1,1}),dp[(reads){1,1}]=1;
while (!s.empty())
{
top=*s.begin(),s.erase(top),tong2[++length2]=(node){top,dp[top]};
if (!top.type)
{
if ((top*2).num<=W) dp[top*2]+=dp[top],s.insert(top*2);
if ((top*3).num<=W) dp[top*3]+=dp[top],s.insert(top*3);
if ((top*5).num<=W) dp[top*5]+=dp[top],s.insert(top*5);
if ((top*7).num<=W) dp[top*7]+=dp[top],s.insert(top*7);
}
else
{
if ((top*2).num<=W) dp[top*2]+=dp[top],s.insert(top*2);
if ((top*3).num<=W) dp[top*3]+=dp[top],s.insert(top*3);
if ((top*4).num<=W) dp[top*4]+=dp[top],s.insert(top*4);
if ((top*5).num<=W) dp[top*5]+=dp[top],s.insert(top*5);
}
}
return;
}
long long getA(long long x)
{
if (x<=N) return A[x]%mod;
if (DSP.find(x)!=DSP.end()) return DSP[x]%mod;
int ps=0;
long long l=(x+7*N-1)/(7*N),r=(x-1)/N;
__int128 res=0;
for (int i=lg[length];i>=0;--i)
if (ps+(1<<i)<=length&&tong[ps+(1<<i)].num.num<l)
ps+=(1<<i);
ps++;
for (int i=ps;i<=length&&tong[i].num.num<=r;i+=8)
{
res+=((!tong[i].num.type)?(__int128)(F[x/tong[i].num.num])*tong[i].data:(__int128)(G[x/tong[i].num.num])*tong[i].data);
if (i+1<=length&&tong[i+1].num.num<=r) res+=((!tong[i+1].num.type)?(__int128)(F[x/tong[i+1].num.num])*tong[i+1].data:(__int128)(G[x/tong[i+1].num.num])*tong[i+1].data);
if (i+2<=length&&tong[i+2].num.num<=r) res+=((!tong[i+2].num.type)?(__int128)(F[x/tong[i+2].num.num])*tong[i+2].data:(__int128)(G[x/tong[i+2].num.num])*tong[i+2].data);
if (i+3<=length&&tong[i+3].num.num<=r) res+=((!tong[i+3].num.type)?(__int128)(F[x/tong[i+3].num.num])*tong[i+3].data:(__int128)(G[x/tong[i+3].num.num])*tong[i+3].data);
if (i+4<=length&&tong[i+4].num.num<=r) res+=((!tong[i+4].num.type)?(__int128)(F[x/tong[i+4].num.num])*tong[i+4].data:(__int128)(G[x/tong[i+4].num.num])*tong[i+4].data);
if (i+5<=length&&tong[i+5].num.num<=r) res+=((!tong[i+5].num.type)?(__int128)(F[x/tong[i+5].num.num])*tong[i+5].data:(__int128)(G[x/tong[i+5].num.num])*tong[i+5].data);
if (i+6<=length&&tong[i+6].num.num<=r) res+=((!tong[i+6].num.type)?(__int128)(F[x/tong[i+6].num.num])*tong[i+6].data:(__int128)(G[x/tong[i+6].num.num])*tong[i+6].data);
if (i+7<=length&&tong[i+7].num.num<=r) res+=((!tong[i+7].num.type)?(__int128)(F[x/tong[i+7].num.num])*tong[i+7].data:(__int128)(G[x/tong[i+7].num.num])*tong[i+7].data);
}
DSP[x]=res;
return res%mod;
}
long long getB(long long x)
{
if (x<=N) return B[x]%mod;
if (DSP2.find(x)!=DSP2.end()) return DSP2[x]%mod;
int ps=0;
long long l=(x+7*N-1)/(7*N),r=(x-1)/N;
__int128 res=0;
for (int i=lg[length2];i>=0;--i)
if (ps+(1<<i)<=length2&&tong2[ps+(1<<i)].num.num<l)
ps+=(1<<i);
ps++;
for (int i=ps;i<=length2&&tong2[i].num.num<=r;i+=8)
{
res+=((!tong2[i].num.type)?(__int128)(F[x/tong2[i].num.num])*tong2[i].data:(__int128)(G[x/tong2[i].num.num])*tong2[i].data);
if (i+1<=length2&&tong2[i+1].num.num<=r) res+=((!tong2[i+1].num.type)?(__int128)(F[x/tong2[i+1].num.num])*tong2[i+1].data:(__int128)(G[x/tong2[i+1].num.num])*tong2[i+1].data);
if (i+2<=length2&&tong2[i+2].num.num<=r) res+=((!tong2[i+2].num.type)?(__int128)(F[x/tong2[i+2].num.num])*tong2[i+2].data:(__int128)(G[x/tong2[i+2].num.num])*tong2[i+2].data);
if (i+3<=length2&&tong2[i+3].num.num<=r) res+=((!tong2[i+3].num.type)?(__int128)(F[x/tong2[i+3].num.num])*tong2[i+3].data:(__int128)(G[x/tong2[i+3].num.num])*tong2[i+3].data);
if (i+4<=length2&&tong2[i+4].num.num<=r) res+=((!tong2[i+4].num.type)?(__int128)(F[x/tong2[i+4].num.num])*tong2[i+4].data:(__int128)(G[x/tong2[i+4].num.num])*tong2[i+4].data);
if (i+5<=length2&&tong2[i+5].num.num<=r) res+=((!tong2[i+5].num.type)?(__int128)(F[x/tong2[i+5].num.num])*tong2[i+5].data:(__int128)(G[x/tong2[i+5].num.num])*tong2[i+5].data);
if (i+6<=length2&&tong2[i+6].num.num<=r) res+=((!tong2[i+6].num.type)?(__int128)(F[x/tong2[i+6].num.num])*tong2[i+6].data:(__int128)(G[x/tong2[i+6].num.num])*tong2[i+6].data);
if (i+7<=length2&&tong2[i+7].num.num<=r) res+=((!tong2[i+7].num.type)?(__int128)(F[x/tong2[i+7].num.num])*tong2[i+7].data:(__int128)(G[x/tong2[i+7].num.num])*tong2[i+7].data);
}
DSP2[x]=res;
return res%mod;
}
int main()
{
long long x;
for (int i=2;i<=M;++i) lg[i]=lg[i>>1]+1;
bfs(),A[0]=read(),B[0]=read(),T=read(),mod=read();
for (int i=1;i<=N;++i)
{
A[i]=max((long long)(i),B[i/2]+B[i/3]+B[i/5]+B[i/7]);
B[i]=max((long long)(i),A[i/2]+A[i/3]+A[i/4]+A[i/5]);
}
for (int i=1;i<=7*N;++i)
{
if (i/2<=N) F[i]+=B[i/2];
if (i/3<=N) F[i]+=B[i/3];
if (i/5<=N) F[i]+=B[i/5];
if (i/7<=N) F[i]+=B[i/7];
}
for (int i=1;i<=5*N;++i)
{
if (i/2<=N) G[i]+=A[i/2];
if (i/3<=N) G[i]+=A[i/3];
if (i/4<=N) G[i]+=A[i/4];
if (i/5<=N) G[i]+=A[i/5];
}
while (T--) x=read(),printf("%lld %lld\n",getA(x),getB(x));
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
1958 920 10 100000000000 0 1 2 3 10 100 200 1000 19580920 20232023