QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#823405 | #434. 密码 | Kevin5307 | 40 | 3ms | 3956kb | C++23 | 3.3kb | 2024-12-20 23:41:53 | 2024-12-20 23:41:53 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define i128 __int128_t
void die(string S){puts(S.c_str());exit(0);}
using range=pair<i128,i128>;
i128 p;
struct equation
{
i128 a,c,err;
equation(){}
equation(i128 _a,i128 _c,i128 _err):a(_a),c(_c),err(_err){}
bool operator <(equation b){return a<b.a;}
equation operator +(equation b){return equation((a+b.a)%p,(c+b.c)%p,err+b.err);}
equation operator -(equation b){return equation((a+p-b.a)%p,(c+p-b.c)%p,err+b.err);}
};
inline bool check(vector<equation> ve)
{
for(auto E:ve) if(E.a!=1) return 0;
return 1;
}
inline int sp(range R)
{
return R.first>R.second;
}
inline range merge(range a,range b)
{
if(!a.first&&a.second==p-1) return b;
if(!b.first&&b.second==p-1) return a;
assert((a.second-a.first+p)%p<p/2);
assert((b.second-b.first+p)%p<p/2);
if(sp(a)&&!sp(b)) swap(a,b);
if(sp(a)&&sp(b)) return range(max(a.first,b.first),min(a.second,b.second));
if(!sp(a)&&!sp(b)) return range(max(a.first,b.first),min(a.second,b.second));
if(a.second>=b.first)
return range(max(b.first,a.first),a.second);
return range(a.first,min(a.second,b.second));
}
vector<equation> evolve(vector<equation> ve)
{
const int limit=100;
srt(ve);
vector<equation> res;
int p=-1;
for(int i=0;i<sz(ve);i++)
{
while(p+1<sz(ve)&&ve[p+1].a<ve[i].a) p++;
if(~p) res.pb(ve[i]-ve[p]);
}
for(auto E:ve) res.pb(E);
srt(res);
while(sz(res)>limit) res.pop_back();
return res;
}
void solve()
{
vector<equation> ve;
int n;
cin>>n;
ll _p;
cin>>_p;
p=_p;
ll err;
cin>>err;
err=(err+1)/2;
for(int i=0;i<n;i++)
{
ll a,b;
cin>>a>>b;
ve.pb(a,b,err);
}
vector<equation> history=ve;
int round=0;
while(!check(ve))
{
round++;
ve=evolve(ve);
for(auto E:ve) history.pb(E);
}
range R(0,p-1);
for(auto E:ve)
{
range nR((E.c-E.err+p)%p,(E.c+E.err)%p);
R=merge(R,nR);
}
for(int i=0;i<10;i++)
{
for(auto E:history)
{
i128 Lb=E.a*R.first;
i128 Rb=E.a*(R.second<R.first?(R.second+p):R.second);
if(Lb/p==Rb/p)
{
i128 dest=(Lb/p*p)+E.c;
i128 Err=E.err;
Lb=max(Lb,dest-Err);
Rb=min(Rb,dest+Err);
Lb=(Lb+E.a-1)/E.a;
Rb=Rb/E.a;
if(Lb>=p) Lb-=p;
if(Rb>=p) Rb-=p;
R=range(Lb,Rb);
}
else if(Lb/p+1==Rb/p)
{
i128 dest=(Lb/p*p)+E.c;
i128 Err=E.err;
if(dest+Err<Lb||dest+p-Err>Rb)
{
if(dest+Err<Lb) dest+=p;
Lb=max(Lb,dest-Err);
Rb=min(Rb,dest+Err);
Lb=(Lb+E.a-1)/E.a;
Rb=Rb/E.a;
if(Lb>=p) Lb-=p;
if(Rb>=p) Rb-=p;
R=range(Lb,Rb);
}
}
}
}
cout<<(ll)(R.first)<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
solve();
return 0;
}
詳細信息
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 3ms
memory: 3680kb
input:
5 50 957125670377996927 1000000 585945225866084424 54589429123661916 573947126284093449 777559102750892238 153166610639441991 900505234633730119 829276546475860347 938947033720938303 667383545481941162 128214790654861522 642730062581689305 607900221293886873 386770285184824777 896008952335357488 399...
output:
557534539406344445 48614680453513616 171635259452156428 510374952494623520 577639061489488455
result:
ok 5 lines
Test #2:
score: 10
Accepted
time: 3ms
memory: 3720kb
input:
5 50 985780537212717349 100000000 930098920826707192 859185097554991862 114822564798967594 461663135509764494 290070133363935573 237203042621528977 876169993923548070 273892762234612214 946247278327197064 419030027202401205 613487802603166556 236215096880543712 249116322858554956 901707011293379960 ...
output:
728806913399199677 304868452402743224 697338918497266871 845921715720531367 470978095570778021
result:
ok 5 lines
Test #3:
score: 10
Accepted
time: 0ms
memory: 3944kb
input:
5 50 912737338345705547 100000000000 560012542306640281 862559101259689768 518735103494826790 725717349992961571 733957671413353056 169708736886932691 847743526385447667 828696564841332397 107464092562234365 59619590616607389 12132877913647855 720538986960211892 612839049922974832 392264373061240257...
output:
667097255749051439 24610618949224203 416899801207925772 297738252979447312 23941143985346380
result:
ok 5 lines
Test #4:
score: 10
Accepted
time: 3ms
memory: 3956kb
input:
5 50 923097614961380909 1000000000000 97787754875250907 894771248217970266 776694885545580577 777083965073479575 75664239830708863 608716256879701985 237252054800496293 227374993084820963 863032115948152221 365706449805636266 24225822787011794 79303911682790301 773238132021550432 520965679721963390 ...
output:
393439798021650097 806593912421988792 494092519607999744 760635090566656810 345872402107725664
result:
ok 5 lines
Test #5:
score: 0
Runtime Error
input:
5 2000 9342566358299507 93425663582995 6219101435016694 607891369107000 2117136321762304 2881084573543566 5176682988483854 9306200956208720 8253904804096661 3103523256778684 781386676390747 6601635146687045 6181577982870712 4562900022641750 771036361995174 8802607411541107 8287347227209040 478889400...
output:
result:
Test #6:
score: 0
Runtime Error
input:
5 2000 9076941054068597 90769410540685 4164033048333799 6936964960910632 3199151620643150 6169865631753216 3651033776359505 2974612359357727 2437435894180490 7336869581147738 5545756515709013 7225793088900941 316850256662501 598866992420596 1931439899610976 2903969210506357 8802893275621192 51018944...
output:
result:
Test #7:
score: 0
Runtime Error
input:
5 50 986015706540181189 9860157065401811 472447084034094466 701096881178679368 789872459992477277 285017390663914121 139210847563896009 338277635649123845 434174656592580245 368068516438243771 537010852985895475 932180742287574181 420177119220393796 66137395305182219 278267742669272495 2098294852103...
output:
result:
Test #8:
score: 0
Runtime Error
input:
5 50 937539041569299811 9375390415692998 243699599186989388 703817656494514633 490867383683796688 57336497141264681 700995121811059804 884266519548391255 881486531050497524 858417683373541541 483378074519689731 77311461506286120 163799445567955662 630863894729944525 440743631644571023 63661909944717...
output:
result:
Test #9:
score: 0
Runtime Error
input:
5 50 956768533438862867 9567685334388628 159562951002452715 368735031805266874 168203674133135151 519955057721789995 47311303546279427 578468091719224380 488003044752475047 477936813372989614 937035830526454154 585363966735703129 36829830576452586 103584638478504234 938044828907421065 95216503175921...
output:
result:
Test #10:
score: 0
Runtime Error
input:
5 50 954878053655579819 9548780536555798 547909457155842874 765505419310330784 582173103744680117 218053461605844877 290855470787761778 22535982680457566 488260659830503816 201490530306797340 811716163899850400 660850073720225447 662289272267072258 744062472352240699 397407312047625799 2689240086486...