結果
| 問題 |
No.1152 10億ゲーム
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2020-08-07 23:19:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,177 bytes |
| コンパイル時間 | 3,000 ms |
| コンパイル使用メモリ | 204,696 KB |
| 最終ジャッジ日時 | 2025-01-12 18:25:10 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 8 TLE * 42 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
const char newl = '\n';
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
vector<T> ts(n);
for(size_t i=0;i<n;i++) cin>>ts[i];
return ts;
}
template<typename T>
struct Dijkstra{
struct Edge{
int to;
T cost;
Edge(int to,T cost):to(to),cost(cost){}
bool operator<(const Edge &o)const{return cost>o.cost;}
};
vector< vector<Edge> > G;
vector<T> ds;
vector<int> bs;
Dijkstra(int n):G(n){}
void add_edge(int u,int v,T c){
G[u].emplace_back(v,c);
}
void build(int s){
int n=G.size();
ds.assign(n,numeric_limits<T>::max());
bs.assign(n,-1);
priority_queue<Edge> pq;
ds[s]=0;
pq.emplace(s,ds[s]);
while(!pq.empty()){
auto p=pq.top();pq.pop();
int v=p.to;
if(ds[v]<p.cost) continue;
for(auto e:G[v]){
if(ds[e.to]>ds[v]+e.cost){
ds[e.to]=ds[v]+e.cost;
bs[e.to]=v;
pq.emplace(e.to,ds[e.to]);
}
}
}
}
T operator[](int k){return ds[k];}
vector<int> restore(int to){
vector<int> res;
if(bs[to]<0) return res;
while(~to) res.emplace_back(to),to=bs[to];
reverse(res.begin(),res.end());
return res;
}
};
//INSERT ABOVE HERE
using P = pair<int, int>;
P pos(int x){
int r=0,c=0;
while(x%2==0) r++,x/=2;
while(x%5==0) c++,x/=5;
return P(r,c);
}
int dr[]={0,0,1,-1};
int dc[]={1,-1,0,0};
int in(int r,int c){
return 0<=r and r<10 and 0<=c and c<10;
}
int idx(int k,int r1,int c1,int r2,int c2){
return (((k*10+r1)*10+c1)*10+r2)*10+c2;
}
int val(int r,int c){
int x=1;
for(int i=0;i<r;i++) x*=2;
for(int j=0;j<c;j++) x*=5;
return x;
}
signed main(){
int x1,x2;
cin>>x1>>x2;
Dijkstra<int> G(20001);
int S=20000;
vector<int> ss;
for(int r1=0;r1<10;r1++){
for(int c1=0;c1<10;c1++){
G.add_edge(S,idx(0,r1,c1,r1,c1),0);
G.add_edge(S,idx(1,r1,c1,r1,c1),0);
for(int r2=0;r2<10;r2++){
for(int c2=0;c2<10;c2++){
G.add_edge(idx(0,r1,c1,r2,c2),idx(1,r1,c1,r2,c2),1);
for(int k=0;k<4;k++){
int nr=r1+dr[k],nc=c1+dc[k];
if(!in(nr,nc)) continue;
G.add_edge(idx(1,nr,nc,r2,c2),idx(0,r1,c1,r2,c2),1);
}
for(int k=0;k<4;k++){
int nr=r2+dr[k],nc=c2+dc[k];
if(!in(nr,nc)) continue;
G.add_edge(idx(1,r1,c1,r2,c2),idx(0,r1,c1,nr,nc),0);
}
}
}
}
}
G.build(S);
while(1){
auto [r1,c1]=pos(x1);
auto [r2,c2]=pos(x2);
cout<<G[idx(0,r1,c1,r2,c2)]<<endl;
for(int k=0;k<4;k++){
int nr=r1+dr[k],nc=c1+dc[k];
if(!in(nr,nc)) continue;
if(val(nr,nc)==x2){
cout<<x2<<endl;
return 0;
}
if(G[idx(1,nr,nc,r2,c2)]<G[idx(0,r1,c1,r2,c2)]){
x1=val(nr,nc);
cout<<x1<<endl;
break;
}
}
cin>>x2;
if(x1==x2) return 0;
}
return 0;
}
beet