Submission #89411

#TimeUsernameProblemLanguageResultExecution timeMemory
89411nicksonaNice sequence (IZhO18_sequence)C++14
100 / 100
918 ms43868 KiB
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
int n,m;
int t;
int M=0;
int v[400001][3];
int mas[400001];
int fix[400001],nmas[1000001];

void top_sort (int u){
	fix[u]=1;
	for (int i=1;i<=2;i++){
		int node=v[u][i];
		if (fix[node]==0) top_sort(node);
	}
	mas[u]=++M;
}

void solve (int sz){
	
	for (int i=0;i<=sz;i++){
		if (i+n<=sz){
			v[i][2]=i+n;
			if(v[i][2]>v[i][1]) swap (v[i][2],v[i][1]);
		}
		if (i+m<=sz){
			v[i+m][2]=i;
			if(v[i+m][2]>v[i+m][1]) swap (v[i+m][2],v[i+m][1]);
		}
	}
	
	for (int i=0;i<=sz;i++){
		if (fix[i]==0){
			top_sort(i);
		}	
	}
	
	for (int i=0;i<=sz;i++){
 		nmas[i]=mas[i]-mas[i-1];
	}
	
	for (int i=0;i<=sz;i++){
		fix[i]=0;
		v[i][1]=0;
		v[i][2]=0;
	}
}

int gcd(int a,int b){
	if (a%b==0){
		return b;
	} 
	else{
		return gcd(b,a%b);
	}
}

main () {
	ios::sync_with_stdio(false);
	cin>>t;
	while (t--){
		cin>>n>>m;
		int ans=m+n-gcd(n,m)-1;
		M=0;
		solve(ans);
		cout<<ans<<endl;
		for (int i=1;i<=ans;i++){
			cout<<nmas[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
}
/*	
  _   _   _          _
 | \ | | (_)        | |
 |  \| |  _    ___  | | __  ___    ___    _ __     __ _
 | . ` | | |  / __| | |/ / / __|  / _ \  | '_ \   / _` |
 | |\  | | | | (__  |   <  \__ \ | (_) | | | | | | (_| |
 |_| \_| |_|  \___| |_|\_\ |___/  \___/  |_| |_|  \__,_|
 
*/

Compilation message (stderr)

sequence.cpp:60:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () {
       ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...