QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#462440 | #1254. Biggest Set Ever | ZSH_ZSH | WA | 457ms | 12708kb | C++14 | 7.7kb | 2024-07-03 19:21:22 | 2024-07-03 19:21:22 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a);i<=(b);i++)
#define drep(i,a,b) for (int i=(a);i>=(b);i--)
using namespace std;
typedef long long ll;
const int mod=998244353;
inline int qmo(int x){return x+((x>>31)&mod);}
inline int add(int x,int y){return qmo(x+y-mod);}
inline int sub(int x,int y){return qmo(x-y);}
inline void inc(int &x,int y){x=add(x,y);}
inline void dec(int &x,int y){x=sub(x,y);}
inline int quick_pow(int x,int k){int res=1; for (;k;k>>=1,x=1ll*x*x%mod) if (k&1) res=1ll*res*x%mod; return res;}
vector<int> __inv{1,1};
inline int getinv(int x)
{
if (x>=(1<<21)) return quick_pow(x,mod-2);
while ((int)__inv.size()<x+1) __inv.push_back(1ll*(mod-mod/__inv.size())*__inv[mod%__inv.size()]%mod);
return __inv[x];
}
mt19937 rng(time(0));
inline int rnd(int l,int r) {return l+rng()%(r-l+1);}
int CipollaVal;
struct CipollaComplex{int x,y;};
inline CipollaComplex operator * (CipollaComplex x,CipollaComplex y)
{
return (CipollaComplex){add(1ll*x.x*y.x%mod,1ll*CipollaVal*x.y%mod*y.y%mod),add(1ll*x.x*y.y%mod,1ll*x.y*y.x%mod)};
}
inline CipollaComplex quick_pow(CipollaComplex x,int k){CipollaComplex res; res.x=1,res.y=0; for (;k;k>>=1,x=x*x) if (k&1) res=res*x; return res;}
inline int Cipolla(int x)
{
int a;
while ((a=rnd(1,1e9))&&(quick_pow(sub(1ll*a*a%mod,x),(mod-1)/2)==1));
CipollaVal=sub(1ll*a*a%mod,x);
int v=quick_pow(CipollaComplex{a,1},(mod+1)/2).x;
return min(v,mod-v);
}
namespace Polynomial
{
const int __G__=3;
vector<int> rt;
inline void init(int lg)
{
rt.resize((1<<lg)+1);
rt[0]=1,rt[1<<lg]=quick_pow(__G__,(mod-1)>>(lg+2));
drep(i,lg,1) rt[1<<(i-1)]=1ll*rt[1<<i]*rt[1<<i]%mod;
rep(i,1,1<<lg) rt[i]=1ll*rt[i&(i-1)]*rt[i&(-i)]%mod;
}
inline void dif(vector<int> &a)
{
int limit=a.size();
for (int len=limit>>1;len;len>>=1)
{
for (int j=0,*w=rt.data();j<limit;j+=(len<<1),w++)
{
for (int k=j,r;k<j+len;k++)
{
r=1ll*(*w)*a[k+len]%mod;
a[k+len]=sub(a[k],r);
inc(a[k],r);
}
}
}
}
inline void dit(vector<int> &a)
{
int limit=a.size();
for (int len=1;len<limit;len<<=1)
{
for (int j=0,*w=rt.data();j<limit;j+=(len<<1),w++)
{
for (int k=j,r;k<j+len;k++)
{
r=add(a[k],a[k+len]);
a[k+len]=1ll*sub(a[k],a[k+len])*(*w)%mod;
a[k]=r;
}
}
}
reverse(a.begin()+1,a.end());
rep(i,0,limit-1) a[i]=1ll*a[i]*getinv(limit)%mod;
}
struct Poly
{
vector<int> a;
Poly () {}
Poly (const vector<int> &x):a(x) {}
Poly (const initializer_list<int> &x):a(x) {}
inline int size() const {return a.size();}
inline void resize(int n) {a.resize(n);}
inline int operator [] (int n) const
{
if (n<0||n>=size()) return 0;
return a[n];
}
inline Poly reverse() const {return Poly(vector<int>(a.rbegin(),a.rend()));}
inline Poly mulxn(int n) const {auto b=a; b.insert(b.begin(),n,0); return Poly(b);}
inline Poly divxn(int n) const {if (n>=size()) return Poly(); return Poly(vector<int>(a.begin()+n,a.end()));}
inline Poly modxn(int n) const {if (!size()) return Poly(); int k=min(size(),n); return Poly(vector<int>(a.begin(),a.begin()+k));}
inline Poly shrink() const {if (!size()) return Poly(); int lst=size()-1; while (lst>=0&&!a[lst]) lst--; return Poly(vector<int>(a.begin(),a.begin()+lst+1));}
inline friend Poly operator + (const Poly &a,const Poly &b)
{
vector<int> res(max(a.size(),b.size()));
rep(i,0,(int)res.size()-1) res[i]=add(a[i],b[i]);
return Poly(res);
}
inline friend Poly operator - (const Poly &a,const Poly &b)
{
vector<int> res(max(a.size(),b.size()));
rep(i,0,(int)res.size()-1) res[i]=sub(a[i],b[i]);
return Poly(res);
}
inline friend Poly operator * (Poly a,Poly b)
{
if (!a.size()||!b.size()) return Poly();
if (a.size()<=40||b.size()<=40)
{
if (a.size()>b.size()) swap(a,b);
vector<int> res(a.size()+b.size()-1);
rep(i,0,(int)(res.size()-1))
{
for (int j=max(0,i-b.size()+1);j<=i&&j<a.size();j++) inc(res[i],1ll*a[j]*b[i-j]%mod);
}
return Poly(res).shrink();
}
int limit=1,sz=a.size()+b.size()-1;
while (limit<sz) limit<<=1; a.a.resize(limit),b.a.resize(limit);
dif(a.a),dif(b.a);
rep(i,0,limit-1) a.a[i]=1ll*a.a[i]*b.a[i]%mod;
dit(a.a);
return a.shrink();
}
inline friend Poly operator * (Poly a,int b) {rep(i,0,a.size()-1) a.a[i]=1ll*a.a[i]*b%mod; return a;}
inline friend Poly operator * (int a,Poly b) {rep(i,0,b.size()-1) b.a[i]=1ll*b.a[i]*a%mod; return b;}
inline Poly& operator += (Poly b) {return (*this)=(*this)+b;}
inline Poly& operator -= (Poly b) {return (*this)=(*this)-b;}
inline Poly& operator *= (Poly b) {return (*this)=(*this)*b;}
inline Poly& operator *= (int b) {return (*this)=(*this)*b;}
inline friend bool operator == (const Poly &a,const Poly &b) {rep(i,0,max(a.size(),b.size())-1) if (a[i]!=b[i]) return false; return true;}
inline Poly deriv() const
{
if (!size()) return Poly();
vector<int> res(size()-1);
rep(i,0,size()-2) res[i]=1ll*a[i+1]*(i+1)%mod;
return Poly(res);
}
inline Poly integ() const
{
vector<int> res(size()+1);
rep(i,0,size()-1) res[i+1]=1ll*a[i]*getinv(i+1)%mod;
return Poly(res);
}
inline Poly inv(int n) const
{
Poly res{getinv(a[0])},tmp;
int k=1;
while (k<n)
{
k<<=1; int limit=k<<1; tmp.resize(limit); res.resize(limit);
rep(i,0,k-1) tmp.a[i]=(*this)[i];
dif(tmp.a),dif(res.a);
rep(i,0,limit-1) res.a[i]=1ll*res[i]*sub(2,1ll*tmp[i]*res[i]%mod)%mod;
dit(res.a);
rep(i,k,limit-1) res.a[i]=0;
rep(i,0,limit-1) tmp.a[i]=0;
}
return res.modxn(n);
}
inline Poly sqrt(int n) const
{
Poly x{Cipolla(a[0])};
int k=1;
while (k<n)
{
k<<=1;
x=(x+(modxn(k)*x.inv(k))).modxn(k)*getinv(2);
}
return x.modxn(n);
}
inline Poly ln(int n) const {return (modxn(n).deriv()*inv(n)).modxn(n).integ().modxn(n);}
inline Poly exp(int n) const
{
Poly res{1};
int k=1;
while (k<n)
{
k<<=1;
res=(res*(Poly{1}-res.ln(k)+modxn(k))).modxn(k);
}
return res.modxn(n);
}
inline Poly pow(int k,int n) const
{
int i=0; while (i<size()&&!a[i]) i++;
if (i==size()||1ll*i*k>=n) return Poly();
Poly x=quick_pow(a[i],mod-2)*divxn(i);
return (x.ln(n-i*k)*k).exp(n-i*k).mulxn(i*k)*quick_pow(a[i],k);
}
inline pair<Poly,Poly> div(const Poly &o) const
{
if (size()<o.size()) return make_pair(Poly(),*this);
Poly f=(reverse().modxn(size()-o.size()+1)*o.reverse().modxn(size()-o.size()+1).inv(size()-o.size()+1)).modxn(size()-o.size()+1).reverse();
Poly g=(modxn(o.size()-1)-o.modxn(o.size()-1)*f.modxn(o.size()-1)).modxn(o.size()-1);
return make_pair(f,g);
}
};
}
using Polynomial::Poly;
inline Poly conv(const Poly &x,const Poly &y)
{
int n=x.size();
assert(x.size()==y.size());
Poly z=x*y;
Poly res; res.resize(n);
rep(i,0,z.size()-1) inc(res.a[i%n],z[i]);
return res;
}
inline Poly quick_pow(Poly x,ll k)
{
int n=x.size();
Poly res; res.resize(n);
res.a[0]=1;
while (k)
{
if (k&1) res=conv(res,x);
k>>=1;
x=conv(x,x);
}
return res;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
Polynomial::init(21);
int n,rem;
string S;
cin>>n>>rem>>S;
ll r=0,s=0;
for (auto &c:S)
{
int v=c-'0';
r=10ll*r+v;
s=(10*s+r/n)%(mod-1);
r%=n;
}
vector<int> f(n);
f[0]=1;
rep(i,0,n-1)
{
vector<int> g=f;
rep(j,0,n-1) inc(g[(i+j)%n],f[j]);
swap(f,g);
}
Poly u{f};
u=quick_pow(u,s+(mod-1));
f=u.a;
rep(i,0,r-1)
{
vector<int> g=f;
rep(j,0,n-1) inc(g[(i+j)%n],f[j]);
swap(f,g);
}
printf("%d\n",f[rem]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 11516kb
input:
3 2 5
output:
8
result:
ok single line: '8'
Test #2:
score: 0
Accepted
time: 3ms
memory: 11392kb
input:
1 0 20
output:
1048576
result:
ok single line: '1048576'
Test #3:
score: 0
Accepted
time: 5ms
memory: 11364kb
input:
10 8 97441781169999
output:
39483594
result:
ok single line: '39483594'
Test #4:
score: 0
Accepted
time: 0ms
memory: 11364kb
input:
10 0 73529553919999
output:
913188246
result:
ok single line: '913188246'
Test #5:
score: 0
Accepted
time: 2ms
memory: 11444kb
input:
10 5 7216893652
output:
803006513
result:
ok single line: '803006513'
Test #6:
score: 0
Accepted
time: 3ms
memory: 11396kb
input:
51 4 490466735660935366104362911237439817660296497884511278059120667639249811034386376211440059814876153833104198879999
output:
754741857
result:
ok single line: '754741857'
Test #7:
score: 0
Accepted
time: 5ms
memory: 11364kb
input:
45 0 6216871967465786523158710331777577058507955388049665933617608862925909208090781993189722633093256714163855609550090284484136100755698161980229368887021285893611742334609577808667250730098679567168835635687562524497440298178123243152474212724715349775392879081815671155873083166544656572426801376...
output:
247716490
result:
ok single line: '247716490'
Test #8:
score: 0
Accepted
time: 6ms
memory: 11392kb
input:
123 95 82762777129999
output:
104574851
result:
ok single line: '104574851'
Test #9:
score: 0
Accepted
time: 6ms
memory: 11328kb
input:
100 8 31437474627210849758566270683758273881261075882083602376365854183768172131884521374556483688326470349999
output:
204425046
result:
ok single line: '204425046'
Test #10:
score: 0
Accepted
time: 6ms
memory: 11484kb
input:
101 65 129135732687243444162224693341284265097302999818949156642879266983062901971745283891629743024085567839999
output:
902554661
result:
ok single line: '902554661'
Test #11:
score: 0
Accepted
time: 6ms
memory: 11388kb
input:
102 0 3488151969475412325389878205822308160017247852281650623347454005909349359794006198664575307015721114499999
output:
162486241
result:
ok single line: '162486241'
Test #12:
score: 0
Accepted
time: 10ms
memory: 11428kb
input:
1012 291 7646813626
output:
980146392
result:
ok single line: '980146392'
Test #13:
score: 0
Accepted
time: 2ms
memory: 11516kb
input:
10 1 8252321334895940615769970904772140913784950414733677250236907211278760730743749190165425246198980003063223759998857676592610546612494049860039116369420896260329913339201912705127782100719073680933781912596330763621573961781964610789874205137283507240907978239171437180129623254976853141357534721...
output:
652741132
result:
ok single line: '652741132'
Test #14:
score: 0
Accepted
time: 457ms
memory: 12708kb
input:
10000 7418 3245239614838766441165109131458206859599048822364196500920217765340625035454604743711500986644932444050563968810069079688108908878846571710567968402401812796927116289077099295526340820005662199516680283500266534222567954309211983031474586099314642398538154929871106845369013158161741156285...
output:
698356153
result:
ok single line: '698356153'
Test #15:
score: 0
Accepted
time: 264ms
memory: 12628kb
input:
9999 779 358336376636226042717427621455961243958393374012413730691317569180699582766828947496902145881339115917085532012414592514320100982450700615324388139090946345231102734023340224881463299658303159811283914016535239877151817544726113079790779414960958512664223829472539894897355539363031791406089...
output:
837883243
result:
ok single line: '837883243'
Test #16:
score: 0
Accepted
time: 456ms
memory: 12392kb
input:
10000 0 5959081123960981456052979042226085365217802503520521998482870583390422819313350263042136451463933153462392827184151729621677652847629006369849703956071611814775739516186470151700231702587051465357283648453972097435393937208562095551505345722276019216674741689533178156201652677931133018670075...
output:
924699691
result:
ok single line: '924699691'
Test #17:
score: 0
Accepted
time: 397ms
memory: 12576kb
input:
10000 9535 4529914918413777622528043117043450937202591529390096894597801955131392043838551780190373994052685563959267673748578051484778843491690348530040553041327206108222053240121197654468570203072462947279505179444683716795754611201162765280673498257918169149948361212426058346508829709396950512100...
output:
984036477
result:
ok single line: '984036477'
Test #18:
score: 0
Accepted
time: 393ms
memory: 12372kb
input:
9240 4639 27361723410151521150732754757747824591816341408004277395827380018571695767504374102641417978743103148415278527868597085535196798439563729486914117045504466181228391680543038904853912910914033179148538533773830859471039264891096812737067663728830538925263207278489348217296058385024096176645...
output:
918206285
result:
ok single line: '918206285'
Test #19:
score: 0
Accepted
time: 287ms
memory: 12564kb
input:
8640 0 36542487284167251663740647042989590271004990959161565599035618320359555884164889579638216196822119364835481192513876617252278990351166475479529866772377957875189299536492571051813538976297743058308068985113568320877021600430398281676260145773030952556440352937340499556103082922663344786146031...
output:
183559339
result:
ok single line: '183559339'
Test #20:
score: 0
Accepted
time: 232ms
memory: 12372kb
input:
8400 300 288271854704449002303660023988228953095431900267967796390634942331317772470120001601892932430552455756800781384921666042619127984502628855577098517482540354318793004328371417736520135839821219973136389836602550635594752488595369449820630558647394225301132623150116886896899160930010237898661...
output:
236192822
result:
ok single line: '236192822'
Test #21:
score: 0
Accepted
time: 414ms
memory: 12628kb
input:
9900 7932 60696943972373113723790595629126483514286522739943462090310067978198194825062752363577899690215576140733032790987877947244269329280647842553127506268616051081441746603578834885861401552062986268589398127724619814495854782291653679848340686262859158150894989658137076297136845970234357040248...
output:
957197274
result:
ok single line: '957197274'
Test #22:
score: 0
Accepted
time: 199ms
memory: 12216kb
input:
8064 0 47334321587030653743112677374440705129805126456129176300634909485205142778452960069919888488272962888344558362309553430584472473664877747506095230385831376281275441466113307422446138481496975295148631099507964104577181484367166622611546731949305572711810831571898086598100505274361166695152297...
output:
669865153
result:
ok single line: '669865153'
Test #23:
score: 0
Accepted
time: 187ms
memory: 12088kb
input:
7560 4251 33923778033803241908219943724011782970171842843279830346925914273149746589031596306410354155061775627084155077246765927736846008968413480151788681683200760666695266835650540805104008248152259180695495822884752048464024168111056913040542477162151971025210582160878305329922703362973968810986...
output:
416930414
result:
ok single line: '416930414'
Test #24:
score: 0
Accepted
time: 3ms
memory: 11376kb
input:
1 0 1
output:
2
result:
ok single line: '2'
Test #25:
score: -100
Wrong Answer
time: 2ms
memory: 11364kb
input:
20 0 1
output:
499122177
result:
wrong answer 1st lines differ - expected: '2', found: '499122177'