QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#5895#564. 魔法Qingyu100 ✓113ms7144kbC++173.0kb2021-01-27 14:57:372021-12-19 07:07:12

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2021-12-19 07:07:12]
  • 评测
  • 测评结果:100
  • 用时:113ms
  • 内存:7144kb
  • [2021-01-27 14:57:37]
  • 提交

answer

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 888888
int ff[SZ];
int gf(int x) {return (~ff[x])?ff[x]=gf(ff[x]):x;}
void ini(int n) {memset(ff,-1,sizeof(ff[0])*(n+1));}
bool uni(int a,int b)
{
	a=gf(a),b=gf(b);
	if(a!=b) return ff[a]=b,1;
	return 0;
}
map<int,int> is;
int gs[SZ],gn=0;
int gid(int x)
{
	int&p=is[x];
	if(!p) gs[p=++gn]=x,ff[gn]=-1;
	return p;
}
ll solve(int n,int p,const vector<pii>& ex)
{
	p%=n;
	if(!p)
	{
		if(n>int(ex.size())+3) return 1e15;
		ini(n);
		for(unsigned _=0;_<ex.size();++_)
			uni(ex[_].fi,ex[_].se);
		for(int i=1;i<n;++i)
			if(gf(i)!=gf(0)) return 1e15;
		return 0;
	}
	bool chk=1;
	if(p<=int(ex.size())+3)
	{
		ini(p);
		for(unsigned _=0;_<ex.size();++_)
			uni(ex[_].fi%p,ex[_].se%p);
		for(int i=1;i<p;++i)
			if(gf(i)!=gf(0)) chk=0;
		if(chk)
		{
			int l=max(n-int(ex.size())-3,0),r=n-p;
			while(l<r)
			{
				int m=(l+r)>>1; gn=0; is.clear();
				for(int i=0;i<p;++i) gid(i);
				for(int i=m;i<n;++i) gid(i);
				for(unsigned _=0;_<ex.size();++_)
					uni(gid(ex[_].fi),gid(ex[_].se));
				for(int i=gn;i;--i) if(gs[i]<m+p)
					uni(gid(gs[i]),gid(gs[i]%p));
				bool go=1;
				for(int i=1;i<=gn;++i)
					if(gf(i)!=gf(1)) go=0;
				if(go) r=m; else l=m+1;
			}
			return l;
		}
	}
	vector<pii> nx;
	for(unsigned _=0;_<ex.size();++_)
		nx.pb(pii(ex[_].fi%p,ex[_].se%p));
	return solve(p,n,nx)+n-p;
}
ll main_()
{
	int n,m,k;
	cin>>n>>m>>k;
	vector<pii> s,s0;
	for(int i=1;i<=k;++i)
	{
		int a,b;
		cin>>a>>b;
		if(n>m) s.pb(pii(b%m,a%m)),s0.pb(pii(b,a));
		else s.pb(pii(a%n,b%n)),s0.pb(pii(a,b));
	}
	if(n>m) swap(n,m);
	if(n>k+10)
		return m+solve(n,m,s);
	if(m>3*k+40)
	{
		ll w=solve(m,n,s0);
		if(w<=m-n) return w+n;
		else return m+solve(n,m,s);
	}
	ini(n+m+3); int su=0;
	for(unsigned _=0;_<s0.size();++_)
	{
		int a=s0[_].fi,b=s0[_].se;
		su+=uni(a+1,b+n+1);
	}
	int c=0;
	while(su<n+m-1)
	{ 
		su+=uni(c%n+1,c%m+n+1), ++c;
		if(c>10000000) return 1e15;
	}
	return c;
}
int main()
{ 
	ll x=main_();
	if(x>1e15) x=1e15;
	printf("%lld\n",x);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 2ms
memory: 5632kb

input:

32768 9987 100
28506 5590
1649 2307
24282 5093
1415 7802
24103 1112
14745 999
10478 6794
10933 9200
31201 3426
25773 2098
30717 4098
3200 4048
26496 9471
29319 5892
13048 3981
11218 7792
11500 4123
4694 8190
18817 9611
27044 352
17568 8736
10759 1283
28346 6950
23288 2224
4457 7966
30596 8780
28335 6001
30312 3535
3940 7591
3710 8503
32099 7618
8585 7795
5220 334
27246 7919
14797 4614
25418 7020
1517 6711
8871 3969
16206 7993
7054 1341
30788 8670
23059 4897
13410 6225
9112 9164
8723 5689
19444 6...

output:

42725

result:

ok answer is '42725'

Test #2:

score: 10
Accepted
time: 36ms
memory: 6052kb

input:

46410 99997 10000
13200 15603
1404 6759
14676 94044
16682 79389
36449 65825
31901 57001
27046 84451
2006 4672
1520 43190
33946 59478
13727 4650
29191 73450
31388 48280
29579 41713
19084 50709
17331 9387
20245 83352
16024 16930
26919 32029
21541 78584
22613 72163
17451 429
35968 91144
32710 49890
24455 35050
30325 23701
38807 72944
28465 50737
35291 21271
40701 68208
29675 87967
34058 2311
2485 73370
27232 92170
850 64
27277 89028
36950 52348
22198 75942
6869 33940
8108 92166
44868 23386
41685 14...

output:

145864

result:

ok answer is '145864'

Test #3:

score: 10
Accepted
time: 0ms
memory: 5612kb

input:

97979 79797 0

output:

177775

result:

ok answer is '177775'

Test #4:

score: 10
Accepted
time: 0ms
memory: 5624kb

input:

1000000000 999999999 0

output:

1999999998

result:

ok answer is '1999999998'

Test #5:

score: 10
Accepted
time: 7ms
memory: 5720kb

input:

989999998 313131313 1024
466297174 81678111
895428400 173894418
780747740 289790809
221247063 162389337
942374253 83165668
931932356 209086772
770316411 298488384
729576515 152933247
950669136 74376768
865258922 262306960
888861710 111995472
172271158 191943242
340448317 260178530
167388442 103292018
320967581 121564311
775672358 188671322
279096826 89842337
875423778 67395250
953584388 278572679
964089156 156082051
705676427 81090330
24263496 144489264
55799897 46038972
919765786 258169071
9033...

output:

1303131082

result:

ok answer is '1303131082'

Test #6:

score: 10
Accepted
time: 43ms
memory: 6304kb

input:

1000000000 999999925 5000
440856479 646493102
382064738 413794943
206899643 720419464
150194558 353738208
946758572 769330019
251755702 969779632
638794254 631234468
262249745 104171843
901250566 795909574
430578068 535877488
105478217 519875979
366439069 862560627
956753461 212752969
313871902 633180029
551662755 835763887
841573996 499862318
857216071 668490557
368239240 254071810
291385151 635394988
491798003 185734651
127442408 382633510
943715936 405089500
960899985 868201022
554377742 1491...

output:

1999999850

result:

ok answer is '1999999850'

Test #7:

score: 10
Accepted
time: 75ms
memory: 6752kb

input:

999999907 1000000000 8192
540774650 94691035
591728528 293583431
592814054 979864335
190750859 433340059
482981971 510387917
982387002 185305067
653699348 228987557
918403337 437980510
154859338 400153583
422956973 887254970
503007876 446370063
414916936 969688761
715623665 376097392
360672986 764026431
841763561 288273503
11749473 481579549
180777572 231155657
746929781 235059060
325647817 657019923
535354258 185350890
459017216 603032188
582018404 252013771
209894522 270482144
789329382 524708...

output:

1999999814

result:

ok answer is '1999999814'

Test #8:

score: 10
Accepted
time: 30ms
memory: 6636kb

input:

562943423 999999000 9999
385328949 980582745
136459932 667607325
290393822 958498858
481953116 904430909
376229043 306789517
15032080 873393376
231481033 112706697
229633813 508410926
60796181 679581742
415339337 492418623
223220093 292682674
344525811 66103339
258918135 931254138
295433344 854614141
51297536 52674879
428101676 425168062
246769845 398857787
205865087 948276240
530491928 218435385
306615385 1154056
285826897 626188431
499596517 751901733
408809410 678182176
146397938 928535693
55...

output:

1562941635

result:

ok answer is '1562941635'

Test #9:

score: 10
Accepted
time: 113ms
memory: 7144kb

input:

1000000000 123456781 10000
247667100 45778348
999825844 106175134
327624972 66128815
491112667 9302003
377454269 45473628
427115223 58846489
993314999 110968063
4636605 79124940
404808490 68772488
756363691 65411607
291323215 13412972
248363734 94512022
153173440 106529309
638926069 45917832
739397845 113831198
507231428 46765659
386682709 112955765
862140065 41898784
438293406 41330253
4079633 62827514
447940738 13991016
383868314 112747795
596206840 73860667
88264611 41723007
25387390 4357952
...

output:

1123456042

result:

ok answer is '1123456042'

Test #10:

score: 10
Accepted
time: 94ms
memory: 6992kb

input:

546546546 546546547 10000
114065011 451061191
98583826 516375080
225494665 363408615
141202892 60461544
462102055 540723225
70095039 191287578
437759149 18141818
200683418 89177806
160074046 490229306
476370689 531079027
312393615 414829931
266614379 19648925
494739799 417483039
125507979 332584267
440193993 23649845
406034534 174161141
422986830 221669287
333576142 476309257
266049837 449453632
364153181 171125806
256469664 270975325
540167421 139141611
44327481 178368989
440388128 204486362
34...

output:

1093093092

result:

ok answer is '1093093092'