QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#67491 | #4124. 随机数生成器 | alpha1022 | 100 ✓ | 22ms | 2224kb | C++23 | 1.7kb | 2022-12-10 16:28:38 | 2022-12-10 16:29:02 |
Judging History
answer
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int T,mod,a,b,c,t;
namespace HASH
{
const int N = 31700;
const int mod = 70921;
int key[N + 5],val[N + 5],pre[N + 5],first[mod + 5],tot;
void clear()
{
tot = 0;
memset(first,0,sizeof first);
}
void insert(int x,int v)
{
int h = x % mod;
int flag = 0;
for(register int i = first[h];i;i = pre[i])
if(key[i] == x)
{
flag = 1;
break;
}
if(!flag)
key[++tot] = x,val[tot] = v,pre[tot] = first[h],first[h] = tot;
}
int query(int x)
{
int h = x % mod;
for(register int i = first[h];i;i = pre[i])
if(key[i] == x)
return val[i];
return -1;
}
}
inline int fpow(int a,int b,int mod)
{
int ret = 1;
for(;b;b >>= 1)
(b & 1) && (ret = (long long)ret * a % mod),a = (long long)a * a % mod;
return ret;
}
inline int bsgs(int a,int b,int mod)
{
HASH::clear();
int lim = ceil(sqrt(mod));
int alim = fpow(a,lim,mod);
for(register int i = 0;i <= lim;++i)
HASH::insert(b,i),b = (long long)b * a % mod;
int prod = alim;
for(register int i = 1,x;i <= lim;++i,prod = (long long)prod * alim % mod)
{
x = HASH::query(prod);
if(~x)
return i * lim - x + 1;
}
return -1;
}
int main()
{
scanf("%d",&T);
for(;T;--T)
{
scanf("%d%d%d%d%d",&mod,&a,&b,&c,&t);
if(c == t)
{
puts("1");
continue;
}
if(!a)
{
puts(b == t ? "2" : "-1");
continue;
}
if(a == 1)
{
if(!b)
puts("-1");
else
printf("%lld\n",(long long)(t - c + mod) * fpow(b,mod - 2,mod) % mod + 1);
continue;
}
int inv = (long long)b * fpow(a - 1,mod - 2,mod) % mod;
printf("%d\n",bsgs(a,(long long)(t + inv) * fpow(c + inv,mod - 2,mod) % mod,mod));
}
}
詳細信息
Test #1:
score: 10
Accepted
time: 1ms
memory: 1664kb
input:
50 102187739 1 0 98635663 98635663 718122103 1 0 75673980 75673980 406441663 1 0 373516519 179271985 572094323 1 0 450255823 11000520 315298601 1 100578433 216519432 311376893 451463167 1 205407670 55950100 37244743 351770801 1 680155 301361592 217442431 22216561 1 13353739 2640981 19855423 18446922...
output:
1 1 -1 -1 185559292 348814060 245604585 3647233 149357091 93814897 1 1 -1 -1 175177161 102633345 32073403 8892407 56991053 16235605 3741553 104593933 77853817 12097364 145267757 128080878 15993658 42824652 21173410 577198665 2095506 312409635 85194781 136196780 107268856 178139446 9154129 62601312 2...
result:
ok 50 lines
Test #2:
score: 10
Accepted
time: 19ms
memory: 2192kb
input:
50 102623957 1 0 86936041 86936041 196338127 1 0 128004150 128004150 423161527 1 0 337398175 87268940 259622497 1 0 242479911 44183211 737805581 1 0 298357500 198398101 687507083 1 0 120669797 213662358 621244067 1 0 192672331 253549661 344688359 1 0 113286489 146949020 708670393 1 0 443430892 33693...
output:
1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 -1 16189959 14745670 -1 113099108 -1 199807465 130296929 77094201 95926080 -1 -1 -1 44402417 27512375 15974515 45227207 264138635 -1 -1 -1 -1 -1 65186581 -1 380406184 -1 -1 -1 122718115 333833952 222370455 30426548 -1 -1 45175004
result:
ok 50 lines
Test #3:
score: 10
Accepted
time: 1ms
memory: 1896kb
input:
50 23 0 4 5 5 3 0 0 2 2 29 0 15 27 15 31 0 15 30 15 59 0 48 27 16 67 0 17 33 45 29 1 0 22 22 47 1 0 7 7 23 1 0 17 5 79 1 0 24 62 29 1 18 16 23 53 1 14 41 21 23 1 2 5 5 23 1 1 15 6 41 1 28 29 7 53 1 5 40 8 41 24 0 0 0 67 37 0 0 0 37 31 0 0 1 29 14 0 0 10 23 20 6 13 8 17 0 2 1 2 47 18 9 11 14 29 24 22...
output:
1 1 2 2 -1 -1 1 1 -1 -1 3 45 1 15 9 37 1 1 -1 -1 -1 2 5 -1 1 38 1 21 -1 -1 -1 35 -1 -1 -1 53 29 22 3 56 30 -1 -1 -1 -1 6 1 7 -1 11
result:
ok 50 lines
Test #4:
score: 10
Accepted
time: 15ms
memory: 2212kb
input:
50 102405847 0 0 13141634 13141634 197771033 0 0 176945843 176945843 556432027 0 0 75983001 396297290 703040029 0 0 641438112 360382963 415087769 0 0 32630586 402090711 168918023 0 0 148638577 131836763 247000597 1 0 50428703 50428703 183436061 1 0 171703712 171703712 105866503 1 0 46387548 23120149...
output:
1 1 -1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 77193721 -1 3046190 -1 -1 26694414 1752817 454450225 -1 10629357 -1 17402535 275006041 606133 58093848 12321227 -1 33969499 11369770 95845179 -1 560107674 82737834 -1 32398260 24252126 110974361 146234548 67896720 -1
result:
ok 50 lines
Test #5:
score: 10
Accepted
time: 1ms
memory: 1680kb
input:
50 102002419 1 0 1779577 1779577 42143183 1 0 41959759 41959759 398098159 1 0 391559262 79657487 191459327 1 0 147084113 80165492 640916033 1 179964357 7599499 303943279 70295737 1 8724628 5836795 38637442 217034177 1 123212050 138688430 66727777 134738911 1 19856795 74032287 110605730 196126831 1 1...
output:
1 1 -1 -1 61466504 17890521 100186159 48188148 53603294 3880359 1 1 -1 -1 77164951 501237012 945411 425301927 153023658 230320986 70220637 119188392 695438769 95887574 44008877 54247971 355817453 207066946 36914083 224803235 427252074 63150493 82778437 420486552 6744540 1675824 111682188 342018082 6...
result:
ok 50 lines
Test #6:
score: 10
Accepted
time: 0ms
memory: 1888kb
input:
50 7 0 4 6 6 67 0 18 24 24 11 0 10 4 10 29 0 3 17 3 41 0 1 36 27 29 0 20 24 22 59 1 0 17 17 79 1 0 76 76 59 1 0 11 56 37 1 0 4 20 29 1 10 18 26 79 1 11 48 15 23 1 1 1 13 31 1 30 0 6 67 1 54 44 38 53 1 44 30 52 47 44 0 0 0 5 4 0 0 0 23 22 0 0 15 67 59 0 0 33 29 23 6 4 27 59 47 38 26 44 29 9 27 4 6 17...
output:
1 1 2 2 -1 -1 1 1 -1 -1 25 77 13 26 53 28 1 1 -1 -1 3 13 -1 16 -1 -1 4 20 17 6 25 -1 2 -1 -1 -1 3 -1 2 14 -1 13 16 56 -1 36 -1 -1 3 -1
result:
ok 50 lines
Test #7:
score: 10
Accepted
time: 22ms
memory: 2224kb
input:
50 102733013 0 55602410 55136097 55136097 230008013 0 44188531 65686043 65686043 585251939 0 401575345 47495968 401575345 83006969 0 38158715 47635496 38158715 776879669 0 119722684 238098409 697098889 190919023 0 30961581 115838854 178126046 165459071 10837 0 0 0 108950197 18555 0 0 0 26569129 2413...
output:
1 1 2 2 -1 -1 1 1 -1 -1 153441670 14628806 170546390 19202106 139449932 -1 -1 241820519 42132584 -1 275022677 -1 -1 -1 194983770 -1 -1 -1 630060540 126128130 -1 -1 179836066 4214567 141848399 77024608 -1 -1 408342919 -1 79112063 168452567 -1 282772862 25819756 162548273 799086 -1 105804969 186680918
result:
ok 50 lines
Test #8:
score: 10
Accepted
time: 17ms
memory: 2220kb
input:
50 102842053 0 80448338 84332169 84332169 331979117 0 99525942 86902228 86902228 236977409 0 166302668 215321622 166302668 143028647 0 126196390 32909893 126196390 181660019 0 157810278 87014476 68783950 647402983 0 301871165 108544861 253533831 138311321 1 0 41769424 41769424 505907789 1 0 41664817...
output:
1 1 2 2 -1 -1 1 1 -1 -1 70261874 31415049 105308527 690327778 257033571 20052364 1 1 -1 -1 192584724 -1 -1 -1 -1 116913337 58517126 1667330 -1 58835454 654005927 66641570 270104958 20604226 646518335 -1 -1 -1 9578315 -1 -1 -1 339358973 187748948 -1 21888714 3482989 -1 -1 -1
result:
ok 50 lines
Test #9:
score: 10
Accepted
time: 18ms
memory: 2204kb
input:
50 102514931 0 0 43242667 43242667 26033071 0 0 10450263 10450263 208124711 0 0 21181664 189910097 763061771 0 0 59714823 363776137 93609949 0 0 79238980 32366103 351692927 0 0 202166385 99115558 219820157 4637 0 0 0 366960551 17627 0 0 0 634562683 15551 0 0 580373382 624821489 10341 0 0 239296472 5...
output:
1 1 -1 -1 -1 -1 1 1 -1 -1 -1 67101006 10284009 -1 -1 -1 210212375 144206412 27116327 22874896 -1 -1 77365742 -1 -1 126823516 4649397 484830407 411730813 141500555 143794014 -1 -1 2836611 23010909 -1 81907034 -1 70944806 -1 -1 -1 -1 1514978 167585351 -1 51884155 63264096 18429499 12895177
result:
ok 50 lines
Test #10:
score: 10
Accepted
time: 1ms
memory: 1664kb
input:
50 102078701 1 0 76734173 76734173 380116237 1 0 311708757 311708757 139124443 1 0 89185586 110173182 118631353 1 0 24928144 48580466 215010989 1 24146111 53295870 142103575 124024921 1 107082533 53083360 33911568 284418907 1 71111559 44090425 250585740 478461353 1 250582546 64055605 432465404 59031...
output:
1 1 -1 -1 65489294 60576802 275561366 353536692 385862949 263459731 84145734 64988229 10639493 231733400 19879575 22437248 207959503 15140302 138281092 136533762 130245705 13165897 374005877 74538641 60059080 126622536 283023707 16231167 427231095 599909333 773153608 48797524 7868593 197025070 50940...
result:
ok 50 lines