https://codeforces.com/contest/1301/problem/D
题意:给出n*m(1<=n,m<=500)格子,要求走k(1e9)步,问能否实现,能则输出步骤(<3000).规则:同一个格子同一方向只能走一步.
解法:根据欧拉图可知,最多可走4*n*m-2*n-2*m步。
要使步骤尽可能少,就要是每一步尽可能多走,比如多走直线。
#include<bits/stdc++.h> typedef long long ll ; #define int ll #define mod 1000000007 #define gcd __gcd #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) //ll lcm(ll a , ll b){return a*b/gcd(a,b);} //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define pb push_back #define mp make_pair #define cin(x) scanf("%lld" , &x); using namespace std; const int maxn = 1e3+9; vector<pair<char , int> >ma; vector<pair<char , int> >v; void insert(char c , int m){ if(m > 0) ma.pb(mp(c , m)); } void solve(){ int n , m , k ; cin >> n >> m >> k; if(4*n*m-2*n-2*m < k){ cout << "NO" << endl; return ; } cout << "YES" << endl; insert('R' , m-1); rep(i , 1 , m-1){ insert('D' , n-1); insert('U' , n-1); insert('L' , 1); } insert('D' , n-1); rep(i , 1 , n-1){ insert('R' , m-1); insert('L' , m-1); insert('U' , 1); } int ans = 0 ; for(auto it : ma){ if(k <= 0){ break; }else{ ans++; v.pb(mp(it.fi , k > it.se ? it.se : k)); k -= it.se ; } } cout << ans << endl; for(auto it : v){ cout << it.se << " " << it.fi << endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //int t ; //cin(t); //while(t--){ solve(); //} }
4nm−