luoguP1080 国王游戏 题解(NOIP2012)(贪心+高精)
#include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define il inline #define rg register #define ll long long #define N 10001 #define inf 1000000010 using namespace std; int n,a,b; struct T{ int le,ri; }h[N]; ll now[N],ans[N],zs[N]; int len1,alen,flag; il void re(rg int &x); int cmp(const T &x,const T &y); void solve(rg int k); void add(rg int k); int main() { re(n),re(a),re(b); for(rg int i=1;i<=n;++i) re(h[i].le),re(h[i].ri); sort(h+1,h+n+1,cmp); while(a) now[++len1]=(a%10),a/=10; for(rg int i=1;i<=n;++i) solve(i),add(i); for(rg int i=len1;i>=1;--i){ if((!flag)&&(!ans[i]))continue; cout<<ans[i]; flag=1; } return 0; } il void re(rg int &x){ rg int res=0,w=1;char c=getchar(); while((c<'0'||c>'9')&&c!='-')c=getchar(); if(c=='-')w=-1,c=getchar(); while(c>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0',c=getchar(); x=w*res; } int cmp(const T &x,const T &y){ return (x.le*x.ri)<(y.le*y.ri); } void solve(rg int k){ memset(zs,0,sizeof(zs)); rg int c=h[k].ri,p=0; for(rg int i=len1;i>=1;--i){ p=p*10+now[i]; zs[i]=p/c,p%=c; } for(rg int i=len1;i>=1;--i){ if(ans[i]==zs[i])continue; if(ans[i]>zs[i])break; memcpy(ans,zs,sizeof(zs)); break; } }//除以自己右手上的值统计 void add(rg int k){ rg int c=h[k].le; for(rg int i=1;i<=len1;++i) now[i]*=c; for(rg int i=1;i<=len1;++i){ if(now[i]>=10){ rg int w=i; while(now[w]>=10){ now[w+1]+=now[w]/10,now[w]%=10,w++; if(w>len1)len1=w; } } } while(now[len1+1]>=10) now[len1+1]=(now[len1]/10),now[len1]%=10,len1++; }
哪怕人间是炼狱,梦想永远是天堂
继续走下去吧,理想永远都年轻,花儿一定会再次盛开