QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#73619 | #1254. Biggest Set Ever | upobir | WA | 632ms | 4564kb | C++14 | 5.2kb | 2023-01-26 19:04:11 | 2023-01-26 19:04:14 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
//7340033 = 7*2^20, 645922817 = 77*2^23, G = 3
//897581057=107*2^23, 998244353=119*2^23, G = 3
namespace NTT {
vector<int> perm, wp[2]; int root, inv, N, invN;
const int mod = 998244353, G = 3; ///G prim root
int power(int a, int p) {
int ans = 1;
while (p) {
if (p & 1) ans = (1LL*ans*a)%mod;
a = (1LL*a*a)%mod; p >>= 1;
} return ans;
}
void precalculate(int n) {
assert( (n&(n-1)) == 0 && (mod-1)%n==0);
N = n; invN = power(N, mod-2);
perm = wp[0] = wp[1] = vector<int>(N);
perm[0] = 0;
for (int k=1; k<N; k<<=1)
for (int i=0; i<k; i++) {
perm[i] <<= 1; perm[i+k] = 1 + perm[i]; }
root=power(G,(mod-1)/N); inv=power(root, mod-2);
wp[0][0]=wp[1][0]=1;
for (int i=1; i<N; i++) {
wp[0][i] = (wp[0][i-1]*1LL*root)%mod;
wp[1][i] = (wp[1][i-1]*1LL*inv)%mod;
}
}
void fft(vector<int> &v, bool invert = false) {
if (v.size()!=perm.size())precalculate(v.size());
for (int i=0; i<N; i++)
if (i < perm[i]) swap(v[i], v[perm[i]]);
for (int len = 2; len <= N; len *= 2) {
for (int i=0, d = N/len; i<N; i+=len) {
for (int j=0, idx=0; j<len/2; j++, idx+=d) {
int x=v[i+j], y =
(wp[invert][idx]*1LL*v[i+j+len/2])%mod;
v[i+j] = (x+y>=mod ? x+y-mod : x+y);
v[i+j+len/2] = (x-y>=0 ? x-y : x-y+mod);
}
}
} if (invert) {
for (int &x : v) x = (x*1LL*invN)%mod; }
}
vector<int> multiply(vector<int> a, vector<int> b){
int n = 1; while (n < a.size()+ b.size()) n<<=1;
a.resize(n); b.resize(n);
fft(a); fft(b);
for (int i=0;i<n;i++) a[i]=(a[i]*1LL*b[i])%mod;
fft(a, true); return a;
}
vector<int> conv(vector<int> a, vector<int> b, int NN){
int n = 1; while (n < a.size()+ b.size()) n<<=1;
a.resize(n); b.resize(n);
fft(a); fft(b);
for (int i=0;i<n;i++) a[i]=(a[i]*1LL*b[i])%mod;
fft(a, true);
for(int i = NN; i<a.size(); i++){
a[i%NN] += a[i];
if(a[i%NN] >= mod) a[i%NN] -= mod;
}
a.resize(NN);
return a;
}
vector<int> selfconv(vector<int> a, int NN){
int n = 1; while (n < a.size()+ a.size()) n<<=1;
a.resize(n);
fft(a);
for (int i=0;i<n;i++) a[i]=(a[i]*1LL*a[i])%mod;
fft(a, true);
for(int i = NN; i<a.size(); i++){
a[i%NN] += a[i];
if(a[i%NN] >= mod) a[i%NN] -= mod;
}
a.resize(NN);
return a;
}
};
const int base = 1e9;
const ll mod = 998244353;
int divide(vector<int>& a, int b){
int carry = 0;
for (int i=(int)a.size()-1; i>=0; --i) {
long long cur = a[i] + carry * 1ll * base;
a[i] = int (cur / b);
carry = int (cur % b);
}
while (a.size() > 1 && a.back() == 0)
a.pop_back();
return carry;
}
vector<int> tonum(string& s){
vector<int> a;
for (int i=(int)s.length(); i>0; i-=9)
if (i < 9)
a.push_back (atoi (s.substr (0, i).c_str()));
else
a.push_back (atoi (s.substr (i-9, 9).c_str()));
return a;
}
int dp[2][10005];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
int rem;
cin>>rem;
assert(0 <= rem && rem < n && n <= 10000);
string ss;
cin>>ss;
assert(ss.size() <= 100000 && 1 <= ss.size());
assert(ss[0] != '0');
for(auto c : ss){
assert(isdigit(c));
}
auto T = tonum(ss);
auto tmp = T;
auto kk = divide(tmp, mod-1);
int pref = divide(T, n);
auto k = divide(T, mod-1);
bool is0 = false;
if(k == 0){
is0 = true;
}
k += mod-1;
if(k == 0){
cerr<<"noo"<<endl;
}
// pref--;
// if(pref < 0){
// pref += n;
// k--;
// if(k < 0)
// k += mod-1;
// }
// auto T = stoll(s);
// auto pref = T % n;
// T /= n;
// auto k = T % (mod-1);
vector<int> P(n), Full(n);
P[0] = 1;
// cout<<k<<" "<<pref<<endl;
dp[0][0] = 1;
int cur = 0;
for(int i = 0; i<n; i++){
cur^=1;
for(int s = 0; s<n; s++){
if(s-i >= 0)
dp[cur][s] = dp[cur^1][s] + dp[cur^1][s-i];
else
dp[cur][s] = dp[cur^1][s] + dp[cur^1][s-i+n];
if(dp[cur][s] >= mod)
dp[cur][s] -= mod;
}
if(i+1 == pref){
for(int j = 0; j<n; j++)
P[j] = dp[cur][j];
}
}
for(int i = 0; i<n; i++){
Full[i] = dp[cur][i];
}
// for(int i = 0; i<n; i++)
// cout<<P[i]<<" ";
// cout<<endl;
//
// for(int i = 0; i<n; i++)
// cout<<Full[i]<<" ";
// cout<<endl;
/// FULL^k
vector<int> ans(n);
ans[0] = 1;
while(k && !is0){
if(k&1){
ans = NTT::conv(ans, Full, n);
}
Full = NTT::selfconv(Full, n);
k >>= 1;
}
ans = NTT::conv(ans, P, n);
ll sum = 0;
for(int i = 0; i<n; i++){
sum = (sum + ans[i]) % mod;
}
assert(sum == NTT::power(2, kk));
assert(ans[rem] >= 0 && ans[rem] < mod);
cout<<ans[rem]<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3484kb
input:
3 2 5
output:
8
result:
ok single line: '8'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3356kb
input:
1 0 20
output:
1048576
result:
ok single line: '1048576'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3440kb
input:
10 8 97441781169999
output:
39483594
result:
ok single line: '39483594'
Test #4:
score: 0
Accepted
time: 2ms
memory: 3368kb
input:
10 0 73529553919999
output:
913188246
result:
ok single line: '913188246'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3484kb
input:
10 5 7216893652
output:
803006513
result:
ok single line: '803006513'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3468kb
input:
51 4 490466735660935366104362911237439817660296497884511278059120667639249811034386376211440059814876153833104198879999
output:
754741857
result:
ok single line: '754741857'
Test #7:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
45 0 6216871967465786523158710331777577058507955388049665933617608862925909208090781993189722633093256714163855609550090284484136100755698161980229368887021285893611742334609577808667250730098679567168835635687562524497440298178123243152474212724715349775392879081815671155873083166544656572426801376...
output:
247716490
result:
ok single line: '247716490'
Test #8:
score: 0
Accepted
time: 2ms
memory: 3380kb
input:
123 95 82762777129999
output:
104574851
result:
ok single line: '104574851'
Test #9:
score: 0
Accepted
time: 2ms
memory: 3400kb
input:
100 8 31437474627210849758566270683758273881261075882083602376365854183768172131884521374556483688326470349999
output:
204425046
result:
ok single line: '204425046'
Test #10:
score: 0
Accepted
time: 2ms
memory: 3420kb
input:
101 65 129135732687243444162224693341284265097302999818949156642879266983062901971745283891629743024085567839999
output:
902554661
result:
ok single line: '902554661'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3368kb
input:
102 0 3488151969475412325389878205822308160017247852281650623347454005909349359794006198664575307015721114499999
output:
162486241
result:
ok single line: '162486241'
Test #12:
score: 0
Accepted
time: 10ms
memory: 3564kb
input:
1012 291 7646813626
output:
980146392
result:
ok single line: '980146392'
Test #13:
score: 0
Accepted
time: 1ms
memory: 3364kb
input:
10 1 8252321334895940615769970904772140913784950414733677250236907211278760730743749190165425246198980003063223759998857676592610546612494049860039116369420896260329913339201912705127782100719073680933781912596330763621573961781964610789874205137283507240907978239171437180129623254976853141357534721...
output:
652741132
result:
ok single line: '652741132'
Test #14:
score: 0
Accepted
time: 565ms
memory: 4520kb
input:
10000 7418 3245239614838766441165109131458206859599048822364196500920217765340625035454604743711500986644932444050563968810069079688108908878846571710567968402401812796927116289077099295526340820005662199516680283500266534222567954309211983031474586099314642398538154929871106845369013158161741156285...
output:
698356153
result:
ok single line: '698356153'
Test #15:
score: 0
Accepted
time: 632ms
memory: 4352kb
input:
9999 779 358336376636226042717427621455961243958393374012413730691317569180699582766828947496902145881339115917085532012414592514320100982450700615324388139090946345231102734023340224881463299658303159811283914016535239877151817544726113079790779414960958512664223829472539894897355539363031791406089...
output:
837883243
result:
ok single line: '837883243'
Test #16:
score: 0
Accepted
time: 561ms
memory: 4564kb
input:
10000 0 5959081123960981456052979042226085365217802503520521998482870583390422819313350263042136451463933153462392827184151729621677652847629006369849703956071611814775739516186470151700231702587051465357283648453972097435393937208562095551505345722276019216674741689533178156201652677931133018670075...
output:
924699691
result:
ok single line: '924699691'
Test #17:
score: 0
Accepted
time: 577ms
memory: 4544kb
input:
10000 9535 4529914918413777622528043117043450937202591529390096894597801955131392043838551780190373994052685563959267673748578051484778843491690348530040553041327206108222053240121197654468570203072462947279505179444683716795754611201162765280673498257918169149948361212426058346508829709396950512100...
output:
984036477
result:
ok single line: '984036477'
Test #18:
score: 0
Accepted
time: 529ms
memory: 4332kb
input:
9240 4639 27361723410151521150732754757747824591816341408004277395827380018571695767504374102641417978743103148415278527868597085535196798439563729486914117045504466181228391680543038904853912910914033179148538533773830859471039264891096812737067663728830538925263207278489348217296058385024096176645...
output:
918206285
result:
ok single line: '918206285'
Test #19:
score: 0
Accepted
time: 371ms
memory: 4388kb
input:
8640 0 36542487284167251663740647042989590271004990959161565599035618320359555884164889579638216196822119364835481192513876617252278990351166475479529866772377957875189299536492571051813538976297743058308068985113568320877021600430398281676260145773030952556440352937340499556103082922663344786146031...
output:
183559339
result:
ok single line: '183559339'
Test #20:
score: 0
Accepted
time: 425ms
memory: 4380kb
input:
8400 300 288271854704449002303660023988228953095431900267967796390634942331317772470120001601892932430552455756800781384921666042619127984502628855577098517482540354318793004328371417736520135839821219973136389836602550635594752488595369449820630558647394225301132623150116886896899160930010237898661...
output:
236192822
result:
ok single line: '236192822'
Test #21:
score: 0
Accepted
time: 625ms
memory: 4432kb
input:
9900 7932 60696943972373113723790595629126483514286522739943462090310067978198194825062752363577899690215576140733032790987877947244269329280647842553127506268616051081441746603578834885861401552062986268589398127724619814495854782291653679848340686262859158150894989658137076297136845970234357040248...
output:
957197274
result:
ok single line: '957197274'
Test #22:
score: 0
Accepted
time: 274ms
memory: 4104kb
input:
8064 0 47334321587030653743112677374440705129805126456129176300634909485205142778452960069919888488272962888344558362309553430584472473664877747506095230385831376281275441466113307422446138481496975295148631099507964104577181484367166622611546731949305572711810831571898086598100505274361166695152297...
output:
669865153
result:
ok single line: '669865153'
Test #23:
score: 0
Accepted
time: 331ms
memory: 4156kb
input:
7560 4251 33923778033803241908219943724011782970171842843279830346925914273149746589031596306410354155061775627084155077246765927736846008968413480151788681683200760666695266835650540805104008248152259180695495822884752048464024168111056913040542477162151971025210582160878305329922703362973968810986...
output:
416930414
result:
ok single line: '416930414'
Test #24:
score: 0
Accepted
time: 2ms
memory: 3368kb
input:
1 0 1
output:
2
result:
ok single line: '2'
Test #25:
score: 0
Accepted
time: 1ms
memory: 3384kb
input:
20 0 1
output:
2
result:
ok single line: '2'
Test #26:
score: 0
Accepted
time: 2ms
memory: 3412kb
input:
20 13 1
output:
0
result:
ok single line: '0'
Test #27:
score: 0
Accepted
time: 603ms
memory: 4380kb
input:
10000 4469 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
290641673
result:
ok single line: '290641673'
Test #28:
score: 0
Accepted
time: 334ms
memory: 4112kb
input:
7560 4041 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
74525042
result:
ok single line: '74525042'
Test #29:
score: 0
Accepted
time: 607ms
memory: 4432kb
input:
9973 5944 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
881015534
result:
ok single line: '881015534'
Test #30:
score: 0
Accepted
time: 524ms
memory: 4376kb
input:
9240 105 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
971384312
result:
ok single line: '971384312'
Test #31:
score: 0
Accepted
time: 471ms
memory: 4028kb
input:
9240 407 3576
output:
801993513
result:
ok single line: '801993513'
Test #32:
score: -100
Wrong Answer
time: 448ms
memory: 4232kb
input:
9240 551 204209679712117827730880226353510653024223856012233043639816521107053027427255530900909664190550923070609753205194078932813278453978969048551211039657884854091997550079122709572652601873661760656527148885540570009859983720628938737170862051434146563351143193685385454224852792784922646291244...
output:
61877843
result:
wrong answer 1st lines differ - expected: '119770088', found: '61877843'