Submission #1006754

#TimeUsernameProblemLanguageResultExecution timeMemory
1006754shenfe1Nice sequence (IZhO18_sequence)C++17
100 / 100
983 ms63260 KiB
#include <bits/stdc++.h> #define ff first #define ss second #define ll long long #define lb lower_bound #define pii pair<int,int> #define pll pair<ll,ll> #define F first #define S second #define ld long double #define pb push_back #define all(v) v.begin(),v.end() #define in insert #define sz(s) (int)s.size() #define int ll #define ppb pop_back #define mem(a,i) memset(a,i,sizeof(a)) using namespace std; const int MAX=5e5+100; const int inf=1e18; const int dx[4]={1,0,-1,0}; const int dy[4]={0,1,0,-1}; int n,m,R; int p[MAX]; int use[MAX]; bool ok; vector<int> g[MAX]; vector<int> top; void dfs(int v){ // cerr<<v<<'\n'; assert(0<=v&&v<=R); use[v]=1; if(v-m>=0&&!use[v-m]){ dfs(v-m); } else if(v-m>=0&&use[v-m]==1){ ok=0; } if(v+n<=R&&!use[v+n]){ dfs(v+n); } else if(v+n<=R&&use[v+n]==1){ ok=0; } top.pb(v); use[v]=2; } bool check(int mid){ R=mid; ok=1; top.clear(); mem(use,0); // cerr<<mid<<" "<<R<<'\n'; for(int i=0;i<=mid;i++){ if(!use[i]){ dfs(i); } } if(!ok)return 0; return 1; } void solve(){ cin>>n>>m; bool isRev=0; if(n<m){ isRev=1; swap(n,m); } int l=min(n,m),r=5e5,res=min(n,m)-1; while(l<=r){ int mid=(l+r)/2; if(check(mid)){ l=mid+1; res=mid; } else r=mid-1; } check(res); for(int i=0;i<=res;i++){ p[top[i]]=i; } for(int i=1;i<=res;i++)p[i]-=p[0]; p[0]=0; for(int i=res;i>=1;i--)p[i]-=p[i-1]; // cout<<isRev<<"\n"; if(isRev)for(int i=1;i<=res;i++)p[i]=-p[i]; cout<<res<<'\n'; for(int i=1;i<=res;i++)cout<<p[i]<<" "; cout<<"\n"; } // #ifdef LOCAL signed main(){ // freopen("pushabox.in","r",stdin); // freopen("pushabox.out","w",stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; cin>>t; while(t--)solve(); } // #endif
#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...