This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
int n,m;
int t;
bool OK=true;
int M=0;
vector <int> v[400001];
int mas[400001];
int fix[400001],nmas[1000001];
void go (int u,int col){
if (!OK) return ;
fix[u]=col;
for (int i=0;i<v[u].size();i++){
int node=v[u][i];
if (fix[node]==col){
OK=false;
return ;
}
if (!OK) return ;
if (fix[node]==0) go(node,col);
}
}
inline bool check (int sz){
for (int i=0;i<=sz;i++){
if (i+n<=sz){
v[i].pb(i+n);
}
if (i+m<=sz){
v[i+m].pb(i);
}
}
OK=true;
for (int i=0;i<=sz;i++){
if (fix[i]==0){
go(i,i+1);
}
}
for (int i=0;i<=sz;i++){
fix[i]=0;
v[i].clear();
}
return OK;
}
void top_sort (int u){
fix[u]=1;
for (int i=0;i<v[u].size();i++){
int node=v[u][i];
if (fix[node]==0) top_sort(node);
}
mas[u]=++M;
}
inline void solve (int sz){
for (int i=0;i<=sz;i++){
if (i+n<=sz){
v[i].pb(i+n);
}
if (i+m<=sz){
v[i+m].pb(i);
}
}
OK=true;
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].clear();
}
}
main () {
ios::sync_with_stdio(false);
cin>>t;
while (t--){
cin>>n>>m;
int k=0,ans=0;
if (n<m){
while (true){
ans=max(k,ans);
k+=n;
ans=max(k,ans);
if (k>=m){
k-=m;
}
if (k==0){
break;
}
}
}
else{
while (true){
ans=max(k,ans);
k+=m;
ans=max(k,ans);
if (k>=n){
k-=n;
}
if (k==0){
break;
}
}
}
ans--;
M=0;
if (ans>0) solve(ans);
cout<<ans<<endl;
for (int i=1;i<=ans;i++){
cout<<nmas[i]<<" ";
}
cout<<endl;
}
return 0;
}
/*
1
2 4
_ _ _ _
| \ | | (_) | |
| \| | _ ___ | | __ ___ ___ _ __ __ _
| . ` | | | / __| | |/ / / __| / _ \ | '_ \ / _` |
| |\ | | | | (__ | < \__ \ | (_) | | | | | | (_| |
|_| \_| |_| \___| |_|\_\ |___/ \___/ |_| |_| \__,_|
*/
Compilation message (stderr)
sequence.cpp: In function 'void go(int, int)':
sequence.cpp:15:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<v[u].size();i++){
~^~~~~~~~~~~~
sequence.cpp: In function 'void top_sort(int)':
sequence.cpp:53:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<v[u].size();i++){
~^~~~~~~~~~~~
sequence.cpp: At global scope:
sequence.cpp:87:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |