QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#356752 | #8303. Junior Mathematician | FOY# | TL | 2385ms | 4264kb | C++14 | 3.9kb | 2024-03-18 10:43:17 | 2024-03-18 10:43:17 |
Judging History
answer
#include <iostream>
#include <vector>
#include <array>
#include <cassert>
using ll = long long;
using namespace std;
using pll = pair<ll, ll>;
using a2 = array<ll, 2>;
const ll mod = 1e9+7;
ll sq(ll x) {
return x*x;
}
ll fastFix(ll x, ll m) {
if (x >= m) x-=m;
return x;
}
ll midFix(ll x, ll m){
return x%m;
}
ll fix(ll x, ll m) {
return ((x%m)+m)%m;
}
ll test(ll sum, ll sumsq, ll m) {
ll x = fix(sum*sum-sumsq, m);
return x/2;
}
ll modPow(ll n, ll p, ll mod) {
ll b = n, o = 1;
while (p) {
if (p&1) o = (o*b)%mod;
b = (b*b)%mod;
p/=2;
}
return o;
}
void solve(string l, string r, int m) {
string s = "";
while (s.size() + l.size() < r.size()) s += "0";
l = s+l;
l = "0" + l;
r = "0" + r;
int om = m;
m *= 2;
// dp[i][j][k][l] -> i=0 => low, i=1 => mid, i=2 => hi
// cnt such that dig sum is j, dig squared sum is k
vector<vector<ll>> dp(m, vector<ll>(m));
vector<a2> top(l.size()), bot(l.size());
vector<ll> topMod(l.size()), botMod(l.size());
vector<ll> pref(l.size());
for (int i = 1; i < l.size(); i++) {
top[i] = top[i-1];
bot[i] = bot[i-1];
top[i][0] = midFix(top[i][0] + sq(r[i]-'0'), m);
top[i][1] = midFix(top[i][1] + r[i]-'0', m);
bot[i][0] = midFix(bot[i][0] + sq(l[i]-'0'), m);
bot[i][1] = midFix(bot[i][1] + l[i]-'0', m);
}
for (int i = 1; i < l.size(); i++) {
ll suf = 2*modPow(10, l.size()-i-1, m);
pref[i] = fix(suf, m);
topMod[i] = topMod[i-1]*10 + r[i]-'0';
topMod[i] = midFix(topMod[i],m);
botMod[i] = botMod[i-1]*10 + l[i]-'0';
botMod[i] = midFix(botMod[i],m);
}
dp[0][0] = 1;
ll out = 0;
if (fix(top.back()[0] + topMod.back()*2 - sq(top.back()[1]), m) == 0) {
out++;
}
if (l != r && fix(bot.back()[0] + botMod.back()*2 - sq(bot.back()[1]), m) == 0) {
out++;
}
if (l == r) {
cout << out << endl;
return;
}
auto test = [&](int x, int d, vector<a2> &prefVals, vector<ll> &prefMod) {
// count total such that top[i][0] - (top[i][1])^2 + j - k = 0
ll curSum = midFix(prefVals[x][1] + d, m);
ll curSq = midFix(prefVals[x][0] + prefMod[x]*pref[x] + d*pref[x+1] + d*d, m);
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
if (fix(sq(curSum + j) - curSq - k, m) == 0) {
out = fastFix((out+dp[j][k]), mod);
}
}
}
};
vector<bool> eq(l.size()+1);
eq[0] = true;
bool eqNow = true;
for (int i =0; i < l.size(); i++) {
if (l[i] != r[i]) eqNow = false;
eq[i+1] = eqNow;
}
ll base = 1;
while (l.size()) {
int lo = l.back() - '0';
int hi = r.back() - '0';
l.pop_back();
r.pop_back();
vector<vector<ll>> ndp(m, vector<ll>(m));
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
for (int l = 0; l < 10; l++) {
ll a = midFix(j+l, m);
ll b = midFix(k+sq(l) + 2*base*l,m);
ndp[a][b] += dp[j][k];
ndp[a][b] = fastFix(ndp[a][b], mod);
}
}
}
base = fix(base*10, m);
int x = r.size()-1;
for (int i = 0; i < 10; i++) {
// count total such that top[i][0] - (top[i][1])^2 + j - k = 0
if (eq[l.size()] && i < hi && i > lo) {
test(x, i, top, topMod);
}
else if (!eq[l.size()]) {
if (i < hi) {
test(x, i, top, topMod);
}
if (i > lo) {
test(x, i, bot, botMod);
}
}
}
dp = ndp;
}
cout << fix(out, mod) << '\n';
//if (out != cnt) assert(out == cnt);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
/*for (int i = 10; i < 100; i++) {
for (int j = i; j < 100; j++) {
for (int k = 2; k <= 20; k++) {
cout << i << ' ' << j << ' ' << k << endl;
solve(to_string(i), to_string(j), k);
}
}
}*/
int t; cin >> t;
while (t--) {
string l, r; cin >> l >> r;
int m; cin >> m;
//int m = fix(rand(), 60)+1;
//string l = to_string(fix(rand(), 10000));
//string r = to_string(fix(rand(), 10000) + stoi(l));
//cout << l <<' ' << r << ' ' << m << endl;
solve(l, r, m);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3556kb
input:
2 10 50 17 33 33 3
output:
2 1
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 73ms
memory: 3812kb
input:
1252 3893 6798 7 5883 8853 7 2999 4351 2 565 1767 7 1759 4751 10 79 8631 2 2128 8721 7 7890 8423 6 4708 7458 9 4501 6027 4 932 2708 2 3518 5859 7 4355 8296 3 2642 4470 10 7408 8939 8 4892 6777 7 4962 7976 6 2722 3171 7 6616 7527 6 7070 7612 5 429 2087 7 8786 8823 3 8831 8994 2 6346 8524 4 6026 8648 ...
output:
505 447 767 143 357 5413 994 150 321 539 1083 388 1267 225 229 348 588 57 150 87 198 21 126 636 281 210 146 1538 635 143 476 700 966 173 275 29 239 48 1018 377 1349 503 646 43 980 685 563 1187 1 85 1 1025 38 16 1372 68 851 679 43 625 955 431 154 185 644 48 677 429 1182 80 518 258 34 281 36 721 148 2...
result:
ok 1252 lines
Test #3:
score: 0
Accepted
time: 1898ms
memory: 4012kb
input:
1253 3862 4458 39 5034 7673 2 510 7150 27 3514 4389 7 6435 6996 58 7300 8758 12 8676 8774 44 8544 8573 49 2445 8608 49 7812 8378 42 4214 4543 50 4995 5544 22 677 6824 55 7444 8746 9 7224 8456 25 3857 5279 26 1343 6042 22 6173 6548 57 2386 6274 58 489 1984 24 1351 2880 47 8499 8624 25 307 8952 46 641...
output:
10 1580 243 143 13 166 4 1 144 17 13 26 107 149 49 82 286 4 83 75 25 5 234 15 200 333 5 31 115 46 17 227 2 129 48 70 142 35 27 244 220 62 251 370 10 375 58 73 211 107 17 79 362 30 1559 397 37 3 24 5 60 85 11 181 19 39 245 176 7 585 138 97 23 21 21 185 69 168 535 61 26 14 39 60 37 50 240 12 44 307 26...
result:
ok 1253 lines
Test #4:
score: 0
Accepted
time: 1906ms
memory: 4132kb
input:
714 5435966 8310614 52 5704997 7881777 18 2219416 7123670 45 8753521 8908557 36 6324833 6708892 16 7826873 8940017 40 4564053 4856296 3 7244416 7308752 50 1082842 3048434 39 1798212 4400110 53 1816169 7932160 12 1357724 3034166 19 7895400 8555848 51 8776920 8789604 46 4322545 5374832 11 6456132 6467...
output:
48977 101411 106247 4030 20946 26428 93477 858 49983 49245 433492 88224 12879 218 95549 823 16450 152429 210336 36228 27704 25600 9019 234595 32348 153999 310550 39190 26443 79160 16566 89981 39522 115033 151913 13156 85511 206261 7650 103807 133667 6639 143061 37525 274112 436224 51814 38402 20615 ...
result:
ok 714 lines
Test #5:
score: 0
Accepted
time: 2024ms
memory: 4040kb
input:
500 47086127 8062735606 18 360878357 562767928 24 49480976 8576022783 13 3476757922 7841781308 36 2555413805 7145295185 18 180566103 8759489364 54 2458610039 3222800330 24 5333648352 6013137590 36 2790561710 8370697212 29 1598694193 3669508654 16 27316449 6307649412 22 739739068 8138716900 19 166733...
output:
461506504 8493277 655830486 125913564 262891609 164175315 32385778 19692674 192417430 133552569 293785322 389425594 487919772 17225005 136379 734106267 73512105 22427516 320865203 272792111 127408900 2921953 11288343 6894189 1171204 857703413 29856027 5021161 6285361 16526239 53011372 122737980 4944...
result:
ok 500 lines
Test #6:
score: 0
Accepted
time: 2172ms
memory: 4260kb
input:
312 1466909309993075 8751933522748891 15 1470437399519306 2056692847618591 31 3462736756734265 3693974732267180 30 2550983922966066 2664700547522419 2 323739377300110 8202701694787388 33 4594231434730263 5457883714482730 48 2342146294298329 2905327391488781 37 576788792669307 7681525011048650 35 214...
output:
304711751 467701491 289116028 831796430 166013031 976516920 81393001 951604794 663155343 12624770 543662565 817025025 907403922 303373776 857468194 802491639 496656087 929134296 202483730 829356695 874995237 840765565 180569984 372753145 458365308 762517477 139702179 357013100 472907393 942235423 55...
result:
ok 312 lines
Test #7:
score: 0
Accepted
time: 2178ms
memory: 4096kb
input:
263 1751756799557614347 2608908010018337253 12 2077506913506405723 2904563726127086387 18 2406248833666099723 3939343666399883893 13 5025873658872423900 5822426817828120268 51 4229271164169909972 4303109259923609466 46 7116626771479640286 7212987115966737012 60 503223518556186695 1973325047576453584...
output:
791235447 178363881 36798026 359581422 56681252 576121209 357107858 927607355 939094762 493883669 593902637 923542274 322126366 522023696 803866720 962490331 677380247 431634331 85697956 996520497 22596944 925288248 748722742 688249621 365871679 414662504 328060332 642808469 151396002 760973240 3086...
result:
ok 263 lines
Test #8:
score: 0
Accepted
time: 200ms
memory: 3664kb
input:
263 328071702552863244 843473407004106590 17 140588221145737224 8119462274765102663 15 7517303140694396850 7601796857178539056 7 6758109157798455543 6880833853610144336 13 111502177553827605 8334471754720816207 4 2103684020252551357 8810675837016588899 4 3941954740850914337 3972920335788395349 7 400...
output:
107319357 488200376 788989796 944936538 25639580 216069069 523290119 773995041 495070125 990777248 204597962 959522884 54204061 485403889 230454072 452413479 47538814 357530945 395864291 140709983 617228930 749613728 133703434 640363996 868940190 51473501 649254312 654406783 639278287 298724448 2400...
result:
ok 263 lines
Test #9:
score: 0
Accepted
time: 622ms
memory: 4008kb
input:
503 33288365082 9227925245195 16 59 802197 26 6956 7375307934047 7 29350276 157525382285705 8 87917723944 2622978617027 29 9588 764826234267569 17 84 7599622 19 377 39561583041 18 652346471168694 688121437333987 24 9666655 404774462995478 2 16643 2613168692 10 910 57602 28 323034 689000707287 10 453...
output:
634214531 26817 609653693 183912932 415716776 780578739 399983 273500099 498943227 71678850 267276803 2070 137041657 431891062 373820027 32802567 588615099 443017075 496773727 766853197 25154 494757948 4990595 419 27065052 119483842 822543680 827289 97446592 250502467 1007321 21592485 726793 1732865...
result:
ok 503 lines
Test #10:
score: 0
Accepted
time: 2385ms
memory: 4264kb
input:
329 412493263678147 660601393707287 28 9723705080777012 19965761235108259510 38 5454 53866130984245 17 18907141353 9709555252366 34 142024114713 9581340733011389 39 66 7512406293686550417 17 537807643448 62409396627207198507 40 72 552764133206412159 11 21861253339 199717447737545826 28 36651874230 9...
output:
695071799 194258371 595465950 176215947 152248536 622533157 950803487 88190259 816965053 109269174 601302145 741903056 664897245 44681370 282931921 871041086 360879395 549817741 382388913 507884463 643110872 383222793 626368166 255489595 672552541 598284827 144731553 114718124 27274235 113797847 727...
result:
ok 329 lines
Test #11:
score: 0
Accepted
time: 2299ms
memory: 3980kb
input:
69 77161831458410772871039809525841542579809898128209163890583315260333136437 7301997025742841615819495645852503066517743746763541025525870768264996909812744190609552664449 30 70734246820445489405694057836634945559878763145819072216802056755958061999531 1270710553508887230166399355899627220531809338...
output:
221403047 93485050 496777672 619168920 968387212 453885434 751747056 83104537 472252591 955950739 280535808 133797866 807949248 927542587 658729246 178667492 174054548 495796128 201947759 115136819 140757396 880065434 613435375 862058374 322246843 293215494 953186103 171576882 533712872 160622124 37...
result:
ok 69 lines
Test #12:
score: 0
Accepted
time: 869ms
memory: 3736kb
input:
17 9471151626496415734718428823075234688507619337255680356350385163270972835704902220481923263174365037406691840830814216 5949394657826153504860981279023067912976461909746558450229326827857816964519427423276619930735353025700929967515167702956696 19 39295126073796699033652586104107912103917679342776...
output:
752882690 700532701 310376229 422115953 568302193 915612968 572482784 233398598 50487870 827481559 60462125 357919212 901960382 623335235 714951138 226806051 428328025
result:
ok 17 lines
Test #13:
score: 0
Accepted
time: 186ms
memory: 3732kb
input:
7 845704762757988157636180239849226688769119506118635304063471171410433893209066584857885106853136395190848783461470382587693305536758193487767879905235120 143459186904784648291738831172680923820108497610869433100832727119148831592249772157345756253435278373049111116275515975630104524357784579619171...
output:
882569526 840333776 687836474 907004095 260240069 937365004 830556269
result:
ok 7 lines
Test #14:
score: 0
Accepted
time: 48ms
memory: 3744kb
input:
2 3539388254371431670620257479446458197086240684315698196756114198594954406965121993031170743859565544985645314370649010814731232678629841611405017952678935313235238592912221255585504398630496719743162966137442590982645930455334134440879067760530007713444032946249144842890977570940269988148362658377...
output:
342758890 312334409
result:
ok 2 lines
Test #15:
score: 0
Accepted
time: 147ms
memory: 3748kb
input:
2 6287879202761133259087501096764479112574362228136890065873316424815822607375966105421071739553594044727464671496645869239252294424678663284707673099297557799544282289355720777062240860264659406586612361864172925421927129280245675950234676492962657274837914254423642637283707917753015976556461798146...
output:
747540616 620345044
result:
ok 2 lines
Test #16:
score: 0
Accepted
time: 10ms
memory: 3772kb
input:
1 9391250929876454672209103553581737568270255945003717382345381154981831841236622988251563840789934104057259194973627062085663243171755099727834199983073453564122064384832913735050167544998115201834027467976606762360874729722263913956425553616596436548920663149123683794339225977708783050476428622179...
output:
939892867
result:
ok single line: '939892867'
Test #17:
score: -100
Time Limit Exceeded
input:
65 540 6638282413084931326456455224476385008365783812975053569531599625141893708083514 42 14 35083743346594998325869037451299605999887746173125052751990439294979 37 10914094351 25855361085321537810417039454198787505944006334366558391477601566609493368576393585779829 43 32 519612713800993875216061448...
output:
264148590 195710997 529764371 141827289 746054751 460077906 63155721 777517330 196257657 961016335 285626144 635717217 487902227 508844707 331290009 239492066 735789731 262472958 281967885 857550310 195621717 402743501 172036152 489537664 26311182 655494741 240493602 431019592 738382481 785627491 18...