[NOIp2012提高组]国王游戏
1 #include<iostream> 2 #include<string.h> 3 #include<algorithm> 4 #include<stdio.h> 5 #define N 10000 6 using namespace std; 7 struct Big_Number 8 { 9 int a[N+10]; 10 Big_Number() 11 { 12 memset(a,0,sizeof(a)); 13 a[0]=1; 14 } 15 Big_Number(int n) 16 { 17 memset(a,0,sizeof(a)); 18 a[0]=0; 19 while(n) 20 { 21 a[++a[0]]=n%10; 22 n/=10; 23 } 24 if(!a[0]) 25 a[0]=1; 26 } 27 Big_Number(char *s) 28 { 29 memset(a,0,sizeof(a)); 30 int len=strlen(s); 31 for(int i=1;i<=len;i++) 32 a[i]=s[len-i]-'0'; 33 a[0]=len; 34 } 35 Big_Number operator*(const Big_Number &b)const 36 { 37 Big_Number c; 38 int i,j,len=a[0]+b.a[0]; 39 for(i=1;i<=a[0];i++) 40 for(j=1;j<=b.a[0];j++) 41 c.a[i+j-1]+=a[i]*b.a[j]; 42 for(i=1;i<len;i++){ 43 c.a[i+1]+=c.a[i]/10; 44 c.a[i]%=10; 45 } 46 while(len>1&&!c.a[len]) 47 len--; 48 c.a[0]=len; 49 return c; 50 } 51 Big_Number operator/(int b)const 52 { 53 Big_Number c; 54 int d=0,i,len=a[0]; 55 for(i=a[0];i;i--){ 56 d=d*10+a[i]; 57 c.a[i]=d/b; 58 d%=b; 59 } 60 while(len>1&&!c.a[len]) 61 len--; 62 c.a[0]=len; 63 return c; 64 } 65 Big_Number operator+(const Big_Number &b)const 66 { 67 Big_Number c; 68 int len=max(a[0],b.a[0]),i; 69 for(i=1;i<=len;i++){ 70 c.a[i]+=a[i]+b.a[i]; 71 c.a[i+1]=c.a[i]/10; 72 c.a[i]%=10; 73 } 74 len++; 75 while(len>1&&!c.a[len]) 76 len--; 77 c.a[0]=len; 78 return c; 79 } 80 Big_Number operator-(const Big_Number &b)const 81 { 82 Big_Number c; 83 int i,len=a[0]; 84 for(i=1;i<=len;i++) 85 { 86 c.a[i]+=a[i]-b.a[i]; 87 if(c.a[i]<0)c.a[i]+=10,c.a[i+1]--; 88 } 89 while(len>1&&!c.a[len])len--; 90 c.a[0]=len; 91 return c; 92 } 93 void operator*=(const Big_Number &x){*this=*this*x;} 94 void operator/=(const int &x){*this=*this/x;} 95 void operator+=(const Big_Number &x){*this=*this+x;} 96 void operator-=(const Big_Number &x){*this=*this-x;} 97 void print(){ 98 for(int i=a[0];i;i--) 99 printf("%d",a[i]); 100 } 101 bool operator>(const Big_Number&b)const 102 { 103 if(a[0]>b.a[0]) 104 return 1; 105 if(a[0]<b.a[0]) 106 return 0; 107 for(int i=a[0];i;i--) 108 if(a[i]>b.a[i]) 109 return 1; 110 else if(a[i]<b.a[i]) 111 return 0; 112 return 0; 113 } 114 bool operator<(const Big_Number&b)const 115 { 116 if(a[0]<b.a[0]) 117 return 1; 118 if(a[0]>b.a[0]) 119 return 0; 120 for(int i=a[0];i;i--) 121 if(a[i]<b.a[i]) 122 return 1; 123 else if(a[i]>b.a[i]) 124 return 0; 125 return 0; 126 } 127 bool operator<=(const Big_Number&b)const{return !(*this>b);} 128 Big_Number operator/(const Big_Number&b)const 129 { 130 Big_Number l(0),r(*this),mid; 131 while(l<r) 132 { 133 mid=(l+r+1)/2; 134 if(mid*b<=*this) l=mid; 135 else r=mid-1; 136 } 137 return l; 138 } 139 void operator/=(const Big_Number&b){*this=*this/b;} 140 }; 141 struct node{int Left,Right;long long Product;}; 142 inline int read() 143 { 144 int sign=1,num=0; 145 char ch=getchar(); 146 while(!isdigit(ch)){if(ch=='-')sign=-1;ch=getchar();} 147 while(isdigit(ch)){num=num*10+(ch-'0');ch=getchar();} 148 return sign*num; 149 } 150 bool comp(node x,node y) 151 { 152 return x.Product<y.Product; 153 } 154 int main() 155 { 156 int n=read(); 157 Big_Number ans(1); 158 node f[N+10]; 159 f[0].Left=read(); 160 f[0].Right=read(); 161 for(int i=1;i<=n;++i) 162 { 163 f[i].Left=read(); 164 f[i].Right=read(); 165 f[i].Product=f[i].Left*f[i].Right; 166 } 167 sort(f+1,f+n+1,comp); 168 for(int i=1;i<=n;++i) 169 ans*=f[i-1].Left; 170 ans/=f[n].Right; 171 if(ans<1)ans=1; 172 ans.print(); 173 }