# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
887415 | pcc | Nice sequence (IZhO18_sequence) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt") #define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 4e5+10;int n,m;vector<int> paths[mxn];int deg[mxn];queue<int> q;int arr[mxn];map<pii,int> mp; inline bool check(int tar){ for(int i = 0;i+n<=tar;i++){ paths[i+n].push_back(i); deg[i]++; } for(int i = 0;i+m<=tar;i++){ paths[i].push_back(i+m); deg[i+m]++; } for(int i = 0;i<=tar;i++){ if(!deg[i])q.push(i); } int p = 0; while(!q.empty()){ auto now = q.front(); arr[now] = ++p; q.pop(); for(auto nxt:paths[now]){ deg[nxt]--; if(!deg[nxt])q.push(nxt); } } for(int i = 0;i<=tar;i++){ paths[i].clear(); deg[i] = 0; } return p == tar+1;} inline void solve(){ cin>>n>>m; int l = 0,r = max(n,m)*2; if(mp.find({n,m}) != mp.end()){ l = r = mp[make_pair(n,m)]; } l = r = n+m-__gcd(n,m)-1; while(l != r){ int mid = (l+r+1)>>1; if(check(mid))l = mid; else r = mid-1; } check(l); cout<<l<<endl; return; mp[make_pair(n,m)] = l; for(int i =1;i<=l;i++)cout<<arr[i]-arr[i-1]<<' ';cout<<'\n'; return;} int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t;cin>>t; while(t--)solve();}