QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#239903 | #6187. Digit Sum Problem | Crysfly | AC ✓ | 317ms | 144076kb | C++17 | 3.6kb | 2023-11-05 00:21:25 | 2023-11-05 00:21:26 |
Judging History
answer
// what is matter? never mind.
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 998244353
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=(__int128)x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,int b){return a.x==b;}
friend bool operator !=(modint a,int b){return a.x!=b;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 200005
#define inf 0x3f3f3f3f
const int N1=(1<<22)+5,N2=4782969+5;
int n,s1,s2,c1,c2,B1,B2,l1,l2;
modint x,y,z,pw2[114],pw3[114];
modint F[N1+N2],G[N1+N2],*f=F+N2,*g=G+N2;
void solveF()
{
int len1=1,len2=1; f[0]=1;
while(len1<B1 || len2<B2){
if(len1<len2){
Rep(i,len1-1,-len2+1)if(f[i].x)f[i+len1]+=f[i]*y;
len1*=2;
}else{
modint w=qpow(x,len2)*z;
For(i,-len2+1,len1-1)
if(f[i].x)f[i-len2]+=f[i]*w,f[i-len2*2]+=f[i]*w*w;
len2*=3;
}
}
}
int cnt2,cnt3,d2[99],d3[99];
void digit(int x){
int t=x; cnt2=cnt3=0;
For(j,0,44)cnt2+=(d2[j]=t%2),t/=2;
For(j,0,28)cnt3+=(d3[j]=x%3),x/=3;
}
void inc(){
++d2[0],++d3[0],++cnt2,++cnt3;
For(j,0,44)if(d2[j]==2)d2[j]=0,++d2[j+1],--cnt2; else break;
For(j,0,28)if(d3[j]==3)d3[j]=0,++d3[j+1],cnt3-=2;else break;
}
void solveG()
{
// B2j2 - B1j1
// 枚举 j2
// += y^{a(j1)}*z^{b(j2)}*x^{j2*B2}
digit(0);
modint xmul=1;
modint xb2=qpow(x,B2);
For(i,0,l2-1){
for(int j=i*B2/B1;j<l1 && i*B2-j*B1>-B2;++j)
g[i*B2-j*B1]+=xmul*pw3[cnt3]*pw2[__builtin_popcountll(j)];
xmul*=xb2,inc();
}
}
signed main()
{
n=read(),x=read(),y=read(),z=read();
*pw2=*pw3=1;
For(i,1,60)pw2[i]=pw2[i-1]*y,pw3[i]=pw3[i-1]*z;
for(int x=1;x<n;x*=2)++s1;
for(int x=1;x<n;x*=3)++s2;
c1=(s1+1)/2,c2=(s2+1)/2,B1=(1ll<<c1),B2=1;
For(i,1,c2)B2*=3; l1=n/B1,l2=n/B2;
solveF();
solveG();
modint res=0;
For(i,-B2+1,B1-1) res+=f[i]*g[i];
res-=1;
int mx=min(l1*B1,l2*B2); digit(mx);
modint mul=qpow(x,mx%(mod-1));
For(i,mx,n){
res+=mul*pw2[cnt2]*pw3[cnt3];
mul*=x,inc();
}
cout<<res.x;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 10ms
memory: 143860kb
input:
123456 12345 234567 3456789
output:
664963464
result:
ok 1 number(s): "664963464"
Test #2:
score: 0
Accepted
time: 314ms
memory: 143864kb
input:
9876543210987 12816 837595 128478
output:
7972694
result:
ok 1 number(s): "7972694"
Test #3:
score: 0
Accepted
time: 280ms
memory: 144064kb
input:
9196665971964 727160879 966835565 746444639
output:
949890012
result:
ok 1 number(s): "949890012"
Test #4:
score: 0
Accepted
time: 278ms
memory: 143716kb
input:
9361549073598 749653880 965489817 371100607
output:
949904373
result:
ok 1 number(s): "949904373"
Test #5:
score: 0
Accepted
time: 313ms
memory: 144032kb
input:
9567572694963 193332930 544713669 390021151
output:
878781872
result:
ok 1 number(s): "878781872"
Test #6:
score: 0
Accepted
time: 317ms
memory: 143864kb
input:
9769301349033 215825931 425927410 408941695
output:
351574791
result:
ok 1 number(s): "351574791"
Test #7:
score: 0
Accepted
time: 305ms
memory: 144076kb
input:
9975324970399 657749333 5151262 729852127
output:
400022780
result:
ok 1 number(s): "400022780"
Test #8:
score: 0
Accepted
time: 12ms
memory: 143812kb
input:
1 149762920 266126484 107367523
output:
910371791
result:
ok 1 number(s): "910371791"
Test #9:
score: 0
Accepted
time: 95ms
memory: 143808kb
input:
903900971479 969144397 356713678 36786741
output:
414279957
result:
ok 1 number(s): "414279957"
Test #10:
score: 0
Accepted
time: 115ms
memory: 144028kb
input:
1940757501452 72683414 106545701 263512239
output:
786323834
result:
ok 1 number(s): "786323834"
Test #11:
score: 0
Accepted
time: 198ms
memory: 143864kb
input:
2914414844884 174466783 133201789 792227626
output:
187534312
result:
ok 1 number(s): "187534312"
Test #12:
score: 0
Accepted
time: 214ms
memory: 144000kb
input:
3851250038782 553074217 881278164 297532837
output:
847958862
result:
ok 1 number(s): "847958862"
Test #13:
score: 0
Accepted
time: 212ms
memory: 143840kb
input:
4692374803740 352867698 211679787 826248223
output:
426334178
result:
ok 1 number(s): "426334178"
Test #14:
score: 0
Accepted
time: 261ms
memory: 143868kb
input:
5566041306806 454651067 959756163 633543322
output:
842296050
result:
ok 1 number(s): "842296050"
Test #15:
score: 0
Accepted
time: 260ms
memory: 143844kb
input:
6902869060611 556434437 709588186 860268821
output:
897681255
result:
ok 1 number(s): "897681255"
Test #16:
score: 0
Accepted
time: 275ms
memory: 144000kb
input:
7239695301792 356227918 736244273 667563920
output:
726280051
result:
ok 1 number(s): "726280051"
Test #17:
score: 0
Accepted
time: 290ms
memory: 143848kb
input:
8217660029470 458011287 486076297 198034954
output:
967159922
result:
ok 1 number(s): "967159922"