結果
問題 | No.438 Cwwプログラミング入門 |
ユーザー |
![]() |
提出日時 | 2016-10-29 14:06:10 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 1,993 bytes |
コンパイル時間 | 979 ms |
コンパイル使用メモリ | 93,036 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-27 22:15:03 |
合計ジャッジ時間 | 4,492 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 98 |
ソースコード
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <stack> #include <queue> #include <vector> #include <set> #include <map> #include <bitset> #include <functional> using namespace std; #define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define rep(i,n) repl(i,0,n) #define mp(a,b) make_pair((a),(b)) #define pb(a) push_back(a) #define all(x) (x).begin(),(x).end() #define dbg(x) cout<<#x"="<<(x)<<endl #define fi first #define se second #define INF 2147483600 // a*x + b*y = gcd(a, b) long extgcd(long a, long b, long& x, long& y){ long d=a; if(b!=0){ d=extgcd(b, a%b, y, x); y-=(a/b)*x; } else { x=1; y=0; } return d; } bool solve(){ long x,y,z; cin>>x>>y>>z; if(z==0){ cout<<"ccW"<<endl; return true; } if(x==0 && y==0) return false; long a,b; long g = extgcd(x,y,a,b); if(z%g!=0){ return false; } a *= z/g; b *= z/g; // x*a + y*b = z // (x*a + n*c) + (y*b -n*c) = z (c := x*y/g = lcm) // とすれば (a+y/g)*x + (b-x/g)*y = z // したがって, |a+n*y/g| + |b-n*x/g| を最小化するnを求める→三分探索 auto func = [&](long n){ return abs(a+n*y/g) + abs(b-n*x/g); }; long l = -INF, r=INF; while(l+2 < r){ long ml = (2*l+r)/3; long mr = (l+2*r)/3; if( func(ml) < func(mr) ) r = mr; else l =ml; } long n; if( func(l+1) < func(l+2) ) n=l+1; else n=l+2; if(func(n)>5000) return false; int bestA = a+n*y/g, bestB = b-n*x/g; int p=-1,m=0; if(bestA<0){ rep(i,-bestA) printf("c"); m += -bestA; } if(bestB<0){ rep(i,-bestB) printf("w"); m += -bestB; } if(bestA>0){ rep(i, bestA) printf("c"); p += bestA; } if(bestB>0){ rep(i, bestB) printf("w"); p += bestB; } rep(i, p) printf("%c", 'C'); rep(i, m) printf("%c", 'W'); printf("\n"); return true; } int main(){ if(!solve()) cout<<"mourennaihasimasenn"<<endl; return 0; }