Submission #433600

# Submission time Handle Problem Language Result Execution time Memory
433600 2021-06-20T08:13:29 Z nathanlee726 Naan (JOI19_naan) C++14
29 / 100
4000 ms 6120 KB
//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
 
//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
 
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
	if(b==0)return 1;
	if(b==1)return(a%mod);
	int tem=po(a,b/2);
	if(b&1)return(((tem*tem)%mod)*a)%mod;
	else return(tem*tem)%mod; 
}
int GCD(int a,int b){
	int x=0;
	int ra,rb;
	while(a&&b){
		if(((a&1)==0)&&((b&1)==0)){
			a>>=1,b>>=1,x++;
		}
		else if((a^b)&1){
			if(a&1)b>>=1;
			else a>>=1;
		}
		else{
			ra=abs(a-b),rb=min(a,b);
			a=ra,b=rb;
		}
	}
	return max(a,b)<<x;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
pii ADD(pii a,pii b){
	int c=a.Y*b.Y/GCD(a.Y,b.Y);
	a.X*=(c/a.Y),b.X*=(c/b.Y);
	return{a.X+b.X,c};
}
pii SUB(pii a,pii b){
	int c=a.Y*b.Y/GCD(a.Y,b.Y);
	a.X*=(c/a.Y),b.X*=(c/b.Y);
	return{a.X-b.X,c};
}
bool LAR(pii a,pii b){
	return a.X*b.Y>=a.Y*b.X;
}
int tab[2005][2005],s[2005];
pii gl[2005],ppo[2005],now[2005];
bool used[2005];
vector<int> v;
vector<pii> o;
signed main(){
	IOS();
	int n,l;
	//freopen("02-04.txt","r",stdin);
	cin>>n>>l;
	bug(n,l);
	F(n)Fi(j,l)cin>>tab[i][j],s[i]+=tab[i][j];
	F(n){
		int g=GCD(s[i],n);
		gl[i]={s[i]/g,n/g};
	}
	F(n)ppo[i]={0,1},now[i]={0,1};
	pii lll={0,1},rr={0,1};
	F(n-1){
		pii mn={3000,1};
		int mip=-1;
		Fi(j,n){
			if(used[j])continue; 
			bug(i,j);
			pii re=SUB(gl[j],now[j]);
			while(re.X>0){
				if(i>3)bug(re.X,re.Y);
				if(ppo[j].Y==1){
					if(LAR(re,{tab[j][ppo[j].X],1})){
						re=SUB(re,{tab[j][ppo[j].X],1});
						ppo[j].X++;
					}
					else{
						int c=GCD(re.X,tab[j][ppo[j].X]);
						ppo[j]=ADD(ppo[j],{re.X/c,tab[j][ppo[j].X]*re.Y/c});
						re={0,1};
						/*if(ppo[j].Y>1e8){
							int x=ppo[j].Y/1e8;
							ppo[j].Y/=x;
							ppo[j].X/=x;
							ppo[j].X++;
						}*/
					}
				}
				else{
					int np=ppo[j].X/ppo[j].Y;
					if(i>3)bug(np);
					pii tmp=SUB({np+1,1},ppo[j]);
					int g=GCD(tab[j][np],tmp.Y);
					if(i>3)bug(g);
					pii ttmp={tmp.X*tab[j][np]/g,tmp.Y/g};
					int c=GCD(re.X,tab[j][np]);
					if(LAR(ttmp,re)){
						ppo[j]=ADD(ppo[j],{re.X/c,re.Y*tab[j][np]/c});
						re={0,1};
						/*if(ppo[j].Y>1e8){
							int x=ppo[j].Y/1e8;
							ppo[j].Y/=x;
							ppo[j].X/=x;
							ppo[j].X++;
						}*/
					}
					else{
						ppo[j]={np+1,1};
						re=SUB(re,ttmp);
					}
				}
			}
			if(LAR(mn,ppo[j]))mn=ppo[j],mip=j;
			now[j]=gl[j];
		} 
		used[mip]=1;
		v.pb(mip);
		o.pb(ppo[mip]);
		bug(i,ppo[mip].X,ppo[mip].Y);
		lll=rr;
		rr=ppo[mip];
		Fi(j,n){
			if(used[j])continue;
			pii ml=lll,mr=rr;
			while(ml!=mr){
				if((ml.X/ml.Y)==(mr.X/mr.Y)){
					pii tmp=(SUB(mr,ml));
					int c=GCD(tmp.Y,tab[j][(ml.X/ml.Y)]);
					pii xx={tmp.X*tab[j][(ml.X/ml.Y)]/c,tmp.Y/c};
					now[j]=SUB(now[j],xx);
					ml=mr;
					/*if(now[j].Y>1e8){
						int x=now[j].Y/1e8;
						now[j].Y/=x;
						now[j].X/=x;
						now[j].X++;						
					}*/
				}
				else{
					if(ml.Y==1){
						now[j]=SUB(now[j],{tab[j][ml.X],1});
						ml.X++;
					}
					else{
						pii tmp={ceiling(ml.X,ml.Y),1};
						tmp=SUB(tmp,ml);
						int c=GCD(tmp.Y,tab[j][(ml.X/ml.Y)]);
						now[j]=SUB(now[j],{tmp.X*tab[j][(ml.X/ml.Y)]/c,tmp.Y/c});
						ml.X=ceiling(ml.X,ml.Y),ml.Y=1;
						/*if(now[j].Y>1e8){
							int x=now[j].Y/1e8;
							now[j].Y/=x;
							now[j].X/=x;
							now[j].X++;						
						}*/
					}
				}
			}
		}
	}
	F(n)if(!used[i])v.pb(i);
	for(pii pi:o)cout<<pi.X<<" "<<pi.Y<<endl;
	for(int i:v)cout<<i+1<<" ";
	cout<<endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 ms 460 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 2 ms 460 KB Output is correct
37 Correct 2 ms 588 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 324 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Execution timed out 4016 ms 6120 KB Time limit exceeded
44 Halted 0 ms 0 KB -