QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#733953 | #2584. 反回文串 | propane | 100 ✓ | 663ms | 9544kb | C++20 | 4.4kb | 2024-11-10 22:22:44 | 2024-11-10 22:22:45 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<random>
#include<map>
#include<unordered_map>
#include<algorithm>
using namespace std;
using LL = long long;
const int s = 9;
// n < 2^78
const LL p[9] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
mt19937_64 rnd(random_device{}());
LL mul(LL a, LL b, LL p){
// return __int128(a) * b % p;
a %= p, b %= p;
LL c = (long double)a * b / p;
LL ans = a * b - c * p;
if (ans < 0)
ans += p;
else if (ans >= p)
ans -= p;
return ans;
}
LL qpow(LL a, LL n, LL p){
LL ans = 1;
a %= p;
while (n){
if (n & 1) ans = mul(ans, a, p);
a = mul(a, a, p);
n >>= 1;
}
return ans;
}
bool check(LL a, LL n, LL x, LL t){
LL ret = qpow(a, x, n);
LL last = ret;
for (int i = 1; i <= t; i++){
ret = mul(ret, ret, n);
if (ret == 1 && last != 1 && last != n - 1)
return true;
last = ret;
}
if (ret != 1) return true;
else return false;
}
bool Miller_Rabin(LL n){
if (n < 2) return false;
for(auto x : p) if (n == x) return true;
if ((n & 1) == 0) return false;
LL x = n - 1;
LL t = 0;
while ((x & 1) == 0){
x >>= 1;
t++;
}
for (int i = 0; i < s; i++){
// LL a = uniform_int_distribution<LL>(1, n - 1)(rnd);
// if (check(a, n, x, t))
if (check(p[i], n, x, t)) return false;
}
return true;
}
LL Pollard_rho(LL x){
LL s = 0, t = 0, c = uniform_int_distribution<LL>(1, x - 1)(rnd);
LL step = 0, goal = 1;
LL val = 1;
for (goal = 1;; goal <<= 1, s = t, val = 1){
for (step = 1; step <= goal; ++step){
t = (mul(t, t, x) + c) % x;
val = mul(val, abs(t - s), x);
if ((step % 127) == 0){
LL d = __gcd(val, x);
if (d > 1)
return d;
}
}
LL d = __gcd(val, x);
if (d > 1) return d;
}
}
LL fac[200], tot;
void findfac(LL n){
if (n == 1) return;
if (Miller_Rabin(n)){
fac[++tot] = n;
return;
}
LL p = n;
while (p >= n) p = Pollard_rho(n);
while (n % p == 0) n /= p;
findfac(n);
findfac(p);
}
void dfs(int u, LL val, const vector<pair<LL, int> > &pri, vector<LL> &f){
if (u == pri.size()){
f.push_back(val);
return;
}
int c = pri[u].second;
for(int i = 0; i <= c; i++){
dfs(u + 1, val, pri, f);
val *= pri[u].first;
}
}
auto go_fac(LL n){
tot = 0;
if (n > 1) findfac(n);
map<LL, int> mp;
vector<pair<LL, int> > pri;
for(int i = 1; i <= tot; i++){
if (mp.contains(fac[i])) continue;
int c = 0;
while(n % fac[i] == 0) c += 1, n /= fac[i];
mp[fac[i]] = c;
}
for(auto x : mp) pri.push_back(x);
vector<LL> f;
dfs(0, 1, pri, f);
sort(f.begin(), f.end());
return make_pair(f, pri);
}
LL qpow2(LL a, LL b, LL p){
a %= p;
LL ans = 1;
while(b){
if (b & 1) ans = ans * a % p;
b >>= 1;
a = a * a % p;
}
return ans;
}
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while(T--){
LL n, m, p;
cin >> n >> m >> p;
auto [factor, primes] = go_fac(n);
const int s = factor.size();
unordered_map<LL, int> mp;
for(int i = 0; i < s; i++){
mp[factor[i]] = i;
}
auto solve = [&](LL len){
LL cnt = n / len;
if (cnt % 2 == 0) return qpow2(m, len, p);
return qpow2(m, (len + 1) / 2, p);
};
vector<LL> f(s);
for(int i = 0; i < s; i++){
f[i] = solve(factor[i]);
}
for(auto [pri, _] : primes){
for(int i = s - 1; i >= 0; i--){
if (factor[i] % pri == 0){
f[i] -= f[mp[factor[i] / pri]];
if (f[i] < 0) f[i] += p;
}
}
}
LL ans = 0;
for(int i = 0; i < s; i++){
ans += f[i] * (factor[i] % p) % p;
}
cout << ans % p << '\n';
}
}
詳細信息
Pretests
Final Tests
Test #1:
score: 3
Accepted
time: 8ms
memory: 3996kb
input:
10 9311702400 2063322107 1032035101 9311702400 3847844404 1014424217 9311702400 6992683534 1067435323 9311702400 1356819643 1024817347 9311702400 6944668829 1042812553 9311702400 9162670852 1003177793 9311702400 6567188538 1062813337 9311702400 3910301217 1018910293 9311702400 2463242636 1018694167 ...
output:
618961590 28597316 1016219343 977847638 108994801 100559666 694084378 492868358 336126742 176095716
result:
ok 10 lines
Test #2:
score: 3
Accepted
time: 8ms
memory: 3720kb
input:
10 8821612800 1108326819 1054253897 8380532160 1267663274 1043024581 8821612800 3176934458 1071245977 9311702400 6481433485 1000759783 8821612800 758922381 1073365919 8380532160 166822173 1001828119 9311702400 7367823578 1015387267 6983776800 1646145481 1030885259 8821612800 3684362555 1007309749 69...
output:
391537248 1030932075 854834302 459335922 896784901 911577797 674524325 392648220 672664689 911456800
result:
ok 10 lines
Test #3:
score: 3
Accepted
time: 7ms
memory: 3696kb
input:
10 5587021440 2127415058 1022963561 8729721000 7659635137 1016643371 7351344000 5657416354 1061115347 8380532160 5792620049 1014814861 7351344000 4242769610 1017904543 5587021440 741231880 1030997497 8031343320 1974187557 1050041869 7351344000 582269195 1046561353 7351344000 5748555565 1019161603 77...
output:
237048366 629142631 260108559 841948025 191823031 162599190 64802834 814233601 189247127 961272597
result:
ok 10 lines
Test #4:
score: 3
Accepted
time: 4ms
memory: 3628kb
input:
10 6469693230 6253588187 1032035101 6469693230 3731448124 1014424217 6469693230 2724819934 1067435323 6469693230 5537386033 1024817347 6469693230 3171489419 1042812553 6469693230 2828773282 1003177793 6469693230 3191696418 1062813337 6469693230 719103207 1018910293 6469693230 513604946 1018694167 64...
output:
938630371 649086455 941467580 785494700 357076968 66986318 592587781 440144505 347253939 264375771
result:
ok 10 lines
Test #5:
score: 3
Accepted
time: 6ms
memory: 3756kb
input:
10 6692786100 1184903319 1054253897 7138971840 6233904554 1043024581 6692786100 2656214258 1071245977 6469693230 2165071435 1000759783 6692786100 1953515781 1073365919 7138971840 2649942813 1001828119 6469693230 2585876408 1015387267 8031343320 1646145481 1030885259 6692786100 2382562055 1007309749 ...
output:
362430310 584760322 869953319 276629173 646549222 879297585 384496639 889650008 891113530 71351606
result:
ok 10 lines
Test #6:
score: 3
Accepted
time: 5ms
memory: 3944kb
input:
10 892371480 808257218 1022963561 2677114440 210273217 1016643371 1338557220 1099583074 1061115347 7138971840 2688719249 1014814861 1338557220 217908770 1017904543 892371480 702433120 1030997497 1784742960 1081816077 1050041869 1338557220 211638935 1046561353 1338557220 841533445 1019161603 35694859...
output:
898680129 155530895 328828577 270244294 651496839 244787553 34900610 479462222 353920192 242259038
result:
ok 10 lines
Test #7:
score: 3
Accepted
time: 4ms
memory: 3728kb
input:
10 6469693230 3833466565 1059357283 6469693230 754927365 1036136159 6469693230 2603192085 1073567071 6469693230 4639206992 1068796243 6469693230 5517015600 1041394999 6469693230 3106392852 1015301891 6469693230 4695615325 1000386287 6469693230 2092246885 1055492153 6469693230 889003093 1014212359 64...
output:
788590260 491779719 689403583 53722625 528848033 388192896 987484818 891368409 178298076 664386522
result:
ok 10 lines
Test #8:
score: 3
Accepted
time: 7ms
memory: 3772kb
input:
10 6692786100 3771774526 1028900737 6692786100 6446972990 1002463597 6692786100 3622542947 1055976967 7138971840 4763520839 1022275453 7138971840 3860802666 1053854443 7138971840 4210388874 1049774177 6692786100 4308388670 1034920309 7138971840 4160615721 1030384631 7138971840 1025055051 1073735731 ...
output:
258209706 955333108 145755558 4472599 691663194 85925478 632174667 301517080 1025049977 566245998
result:
ok 10 lines
Test #9:
score: 3
Accepted
time: 6ms
memory: 3980kb
input:
10 5354228880 3994707641 1050182537 892371480 431984969 1059422839 5354228880 4963791513 1029557359 892371480 294323989 1030188781 3569485920 1712959161 1065566561 892371480 385935628 1041335231 2677114440 1536725174 1071565279 892371480 738654189 1023834521 5354228880 4834381361 1067648243 80313433...
output:
769578319 245408447 363380756 145407027 690436473 437720110 849793084 484508795 891127810 808075938
result:
ok 10 lines
Test #10:
score: 3
Accepted
time: 0ms
memory: 3524kb
input:
10 7979869234 880839352 1035297457 8978314027 7725687764 1010851649 6003473974 3614058927 1065086513 8093088763 558240262 1036101541 7514563163 4488380730 1024457293 8987295623 1665299298 1050362507 6725389078 6724791537 1066818989 6694198723 5223088598 1023481961 9302162851 1649517328 1053299347 71...
output:
637682663 411426186 798331661 1021488669 191534605 218895374 714845656 838599880 413147483 759261190
result:
ok 10 lines
Test #11:
score: 3
Accepted
time: 77ms
memory: 4840kb
input:
10 97821761637600 65704306750166 1046841041 97821761637600 59463780551379 1010287783 97821761637600 7149419822967 1001388403 97821761637600 78654751729995 1017108451 97821761637600 63496010671264 1068952721 97821761637600 94997930077494 1036598287 97821761637600 90164458929349 1069966747 97821761637...
output:
937975310 584792240 231594977 989139483 276771022 503389932 846237376 161147283 961859777 724963376
result:
ok 10 lines
Test #12:
score: 3
Accepted
time: 73ms
memory: 4580kb
input:
10 83847224260800 65393073767891 1008804857 93163582512000 59924705614375 1008420961 83847224260800 17416796259944 1022373137 97821761637600 82478701012930 1051034311 83847224260800 5858773437789 1051518067 93163582512000 39422697574754 1062876083 97821761637600 38428821290002 1041057107 93163582512...
output:
56184649 596420001 494650147 90036534 374621716 421195622 687855255 187265420 224915504 688864198
result:
ok 10 lines
Test #13:
score: 3
Accepted
time: 65ms
memory: 4568kb
input:
10 86952677011200 31235848972059 1049970937 86642131736160 28251471830580 1016834059 80955940665600 6124775121092 1065935653 86642131736160 12615181180615 1004490881 93163582512000 81931479686275 1008289823 65214507758400 8862771921575 1004938801 86952677011200 14455900391733 1037240747 989461497024...
output:
270703194 90018264 843520812 583177614 66525787 33478275 380053450 791995644 196152868 293333471
result:
ok 10 lines
Test #14:
score: 3
Accepted
time: 57ms
memory: 4332kb
input:
10 89048857617720 57552493280366 1046841041 89048857617720 5584175331939 1010287783 89048857617720 18794867636967 1001388403 89048857617720 19961694747435 1017108451 89048857617720 67300190290504 1068952721 89048857617720 83507754901014 1036598287 89048857617720 72230469295789 1069966747 89048857617...
output:
285162165 992920507 613012802 511614681 1025574201 923288434 797130041 466760148 878381276 50499538
result:
ok 10 lines
Test #15:
score: 3
Accepted
time: 48ms
memory: 4304kb
input:
10 29682952539240 12031044006851 1008804857 59365905078480 28921935656215 1008420961 29682952539240 8799164877584 1022373137 89048857617720 87680334369850 1051034311 29682952539240 8317256865189 1051518067 59365905078480 38439304203794 1062876083 89048857617720 33537733208122 1041057107 593659050784...
output:
632625938 622664420 154768854 88437443 620002944 387993011 648216718 979385570 370715311 1039138043
result:
ok 10 lines
Test #16:
score: 3
Accepted
time: 54ms
memory: 4492kb
input:
10 96269035262400 71606734727259 1049970937 77015228209920 47505278883060 1016834059 86642131736160 77443103802692 1065935653 77015228209920 31868988233095 1004490881 59365905078480 5433826934755 1008289823 14841476269620 6249015856655 1004938801 96269035262400 14455900391733 1037240747 742073813481...
output:
683797105 991850589 115872044 364381822 499017184 36862841 469104118 182955049 831957097 843215726
result:
ok 10 lines
Test #17:
score: 3
Accepted
time: 54ms
memory: 4624kb
input:
10 89048857617720 59723364969244 1038174637 89048857617720 4324272870644 1073271281 89048857617720 31842282833961 1030927823 89048857617720 4236254270692 1040321327 89048857617720 75440686804380 1051681867 89048857617720 13202781823767 1045381861 89048857617720 8887626540892 1001900093 8904885761772...
output:
479917539 762627203 56790269 358922414 273147925 223722863 360109472 975975659 719119603 111015222
result:
ok 10 lines
Test #18:
score: 3
Accepted
time: 54ms
memory: 4316kb
input:
10 59365905078480 34450079405369 1012341877 59365905078480 46760618169023 1003287847 59365905078480 51289992595259 1040994763 89048857617720 79105592669341 1057715303 89048857617720 6170835681130 1045728847 89048857617720 60666028852502 1046728999 59365905078480 10515727173552 1020354001 89048857617...
output:
817546647 962470742 798730759 119276831 494599677 19851510 905439195 839053358 345307298 281588296
result:
ok 10 lines
Test #19:
score: 3
Accepted
time: 48ms
memory: 4392kb
input:
10 44524428808860 25396419053547 1039485857 59365905078480 23800715431124 1011919663 44524428808860 38594252365929 1068321997 59365905078480 47553672393134 1022517227 29682952539240 4126110370920 1073036683 89048857617720 84607210887856 1010508133 89048857617720 35379350268090 1032317971 44524428808...
output:
274715926 252258875 512398159 760247695 443523502 1009975639 436167870 613637989 460598323 394586949
result:
ok 10 lines
Test #20:
score: 3
Accepted
time: 40ms
memory: 4536kb
input:
10 14841476269620 13090676589743 1012553497 14841476269620 264453710949 1038849001 59365905078480 17627230584068 1056555481 29682952539240 10091814009426 1017716317 96269035262400 94345568750137 1052040307 29682952539240 2727262057754 1004833033 14841476269620 8169685928294 1053142921 77015228209920...
output:
582486331 553917379 78378867 231359269 630052741 811694173 880163209 210735871 592990019 476730798
result:
ok 10 lines
Test #21:
score: 3
Accepted
time: 663ms
memory: 9544kb
input:
10 897612484786617600 284495640150868270 1068847981 897612484786617600 335478391619138595 1016494901 897612484786617600 13834456571793948 1018602829 897612484786617600 9691686369089746 1040949647 897612484786617600 226142799520103675 1027346459 897612484786617600 791849793029350991 1039182773 897612...
output:
457168573 472221051 468339435 151258078 849570775 373822984 425358682 740061191 190938348 160636186
result:
ok 10 lines
Test #22:
score: 3
Accepted
time: 653ms
memory: 9476kb
input:
10 897612484786617600 750628014753890119 1022274893 961727662271376000 343339759123245095 1065063511 876240758958364800 89567842372889284 1048348559 897612484786617600 739359685926600051 1020838303 748010403988848000 747129712979718149 1054522943 876240758958364800 720096812936730964 1007826761 8762...
output:
1006139892 541194829 383805259 441539759 571065282 900165876 978545677 773223800 227933803 871953717
result:
ok 10 lines
Test #23:
score: 3
Accepted
time: 621ms
memory: 9536kb
input:
10 800573297242118400 68951444425902091 1017799529 970391875444992000 199763227435190697 1029579563 822811444387732800 260851727787862000 1067951513 961727662271376000 578874066870744950 1027562453 876240758958364800 258875322639262541 1019814031 897612484786617600 797990419793632458 1043657123 7480...
output:
840284005 575943149 915540875 68757590 319716110 218297899 74296337 1007971847 610976707 594636009
result:
ok 10 lines
Test #24:
score: 3
Accepted
time: 195ms
memory: 5732kb
input:
10 614889782588491410 185606883766390210 1068847981 614889782588491410 469867959240547695 1016494901 614889782588491410 148224024193203048 1018602829 614889782588491410 193525632182737876 1040949647 614889782588491410 176698421327864645 1027346459 614889782588491410 360793956254507711 1039182773 614...
output:
441700083 974347653 262659363 115319026 212003175 741460576 794813085 293394323 421208375 263674915
result:
ok 10 lines
Test #25:
score: 3
Accepted
time: 397ms
memory: 7800kb
input:
10 614889782588491410 548624160707403577 1022274893 941958815880242160 343339759123245095 1065063511 837296725226881920 89567842372889284 1048348559 614889782588491410 541582173157643931 1020838303 784965679900201800 747129712979718149 1054522943 837296725226881920 836928914131179604 1007826761 8372...
output:
759594331 993251321 781693177 323772251 532189358 644533459 586297154 837945148 442901990 405569292
result:
ok 10 lines
Test #26:
score: 3
Accepted
time: 430ms
memory: 8144kb
input:
10 418648362613440960 230535811145998411 1017799529 392482839950100900 134871880853629197 1029579563 627972543920161440 563617843688110000 1067951513 941958815880242160 638180606044146470 1027562453 837296725226881920 570427592491125581 1019814031 614889782588491410 133656259012902018 1043657123 784...
output:
440169170 823366712 642549401 737079582 15176364 148597990 485884416 478246206 610970646 377452274
result:
ok 10 lines
Test #27:
score: 3
Accepted
time: 196ms
memory: 5432kb
input:
10 614889782588491410 324528924460811372 1038327863 614889782588491410 139062845439531100 1006573649 614889782588491410 149429622085111161 1019605849 614889782588491410 7704136468202542 1042571897 614889782588491410 194109512776789555 1035103057 614889782588491410 197195421925608386 1002932869 61488...
output:
607449540 462251823 519639392 513705390 993398348 906521984 435956154 64350492 203368884 486514451
result:
ok 10 lines
Test #28:
score: 3
Accepted
time: 378ms
memory: 7668kb
input:
10 614889782588491410 146649548795430295 1067347297 784965679900201800 641086776380066458 1027646071 837296725226881920 761625763848323325 1018927451 784965679900201800 45331870490726382 1028887193 614889782588491410 86844596412647205 1025448899 941958815880242160 47322812242258689 1045401569 614889...
output:
682385411 262134291 317826658 185951453 415431054 231734034 830897915 738601535 260458924 788945003
result:
ok 10 lines
Test #29:
score: 3
Accepted
time: 416ms
memory: 8088kb
input:
10 837296725226881920 746214135604946978 1065739093 837296725226881920 666721954105236687 1013602927 313986271960080720 253103342166514610 1025189831 470979407940121080 8057809644202984 1017206623 941958815880242160 809534725951162510 1071436021 627972543920161440 189974394931876589 1073188141 78496...
output:
425252512 506664460 810973458 622148610 365806829 104978690 487663789 105824880 425218595 96144561
result:
ok 10 lines
Test #30:
score: 3
Accepted
time: 1ms
memory: 3640kb
input:
10 583319503545642502 512749524262429710 1016677360 998073919231328211 606822131952824549 1049557159 838270951561095909 344603458850435590 1045551183 792962702528464879 574929391996695121 1047345261 779189632161192841 2457787630059798 1036751830 701317505073449939 377307445983541927 1061278496 51829...
output:
917645780 583978911 209015992 587468785 90687328 924940421 770172601 999811269 258145824 74012699
result:
ok 10 lines
Test #31:
score: 3
Accepted
time: 1ms
memory: 3564kb
input:
10 505834248917738537 141616407693594960 1009821467 952544052832526078 250297268803379656 1040340227 738023676321505814 512476453624870754 1021112857 838845337456425128 535716618777509511 1019130793 585915921539974217 335652732109197511 1039612003 777449414933486322 732354432144283440 1057518331 768...
output:
335260513 247346826 224322183 853323773 533116010 426181477 972302899 306861660 913023257 107380372
result:
ok 10 lines
Test #32:
score: 3
Accepted
time: 14ms
memory: 3568kb
input:
10 999999822000007597 558103808373592802 1071304436 999999822000007597 514302866459361322 1072161713 999999812000008307 271935935809992630 1018128744 999999680000023751 477844293863885798 1007553250 999999830000006741 52166157770476248 1069531067 999999812000008307 263390963057727865 1033763571 9999...
output:
1068194904 979523748 126018720 230965172 654803417 944853826 244893616 402852528 272080445 214916791
result:
ok 10 lines
Test #33:
score: 4
Accepted
time: 12ms
memory: 3580kb
input:
10 999999726000014413 575878802009182398 1023868099 999999734000012789 44080224144018795 1015956811 999999866000004473 521838889502779856 1034478097 999999776000012519 471938167834409026 1021704011 999999812000008307 962126809746718079 1011420203 999999726000014413 849691353492228173 1009404439 9999...
output:
973078113 354818059 318341605 250107418 177689029 184526912 855020515 236059270 17353273 340876170
result:
ok 10 lines