# noip2012 国王游戏

### 1198 国王游戏

2012年NOIP全国联赛提高组

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond

3

1 1

2 3

7 4

4 6

2

【输入输出样例说明】

【数据范围】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 10010, maxm = 50000, inf = 100000000;

int n,lens, ans[maxm], sum[maxm], t[maxm],lena,lent;

struct node
{
int a, b;
}s[maxn];

bool cmp(node x, node y)
{
return x.a * x.b < y.a * y.b;
}

void cheng(int x)
{
int jinwei = 0;
for (int i = 0; i < lens; i++)
{
sum[i] = sum[i] * x + jinwei;
jinwei = sum[i] / inf;
sum[i] %= inf;
}
while (jinwei > 0)
{
sum[lens++] = jinwei % inf;
jinwei /= inf;
}
}

void chu(int x)
{
int buwei = 0;
for (int i = lens - 1; i >= 0; i--)
{
buwei = buwei * inf + sum[i];
t[i] = buwei / x;
buwei -= x * t[i];
}
for (int i = lens - 1; i >= 0; i--)
if (t[i] > 0)
{
lent = i + 1;
break;
}
}

void fuzhi()
{
for (int i = lent - 1; i >= 0; i--)
ans[i] = t[i];
}

void gengxin()
{
if (lent > lena)
{
fuzhi();
lena = lent;
}
else if (lent == lena)
for (int i = lent - 1; i >= 0; i--)
{
if (t[i] > ans[i])
{
fuzhi();
break;
}
else if (t[i] < ans[i])
break;
}
}

void huifu(int x)
{
int h = inf;
while (h >= 10)
{
h /= 10;
if (x < h)
printf("0");
}
}

int main()
{
scanf("%d", &n);
for (int i = 0; i <= n; i++)
scanf("%d%d", &s[i].a, &s[i].b);
sort(s + 1, s + n + 1, cmp);
sum[0] = 1;
lens = 1;
for (int i = 0; i < n; i++)
{
cheng(s[i].a);
chu(s[i + 1].b);
gengxin();
}
for (int i = lena - 1; i >= 0; i--)
{
if (i != lena - 1)
huifu(ans[i]);
printf("%d", ans[i]);
}
printf("\n");

return 0;
}

AC代码写不出来，借用一下codevs一位大神的代码：

#include<stdio.h>
#include<algorithm>
using namespace std;
int n;//丞相人数
int ans[5000];//答案
int tt[5000];
int len;//ans长度
int maxp;
int maxans[5000];
void gx(int a)//高精乘
{
int t[5000];
t[1] = 0;
for (int i = 1;i <= len || (i>len&&t[i] != 0);i++)
{
ans[i] *= a;
ans[i] += t[i];
t[i + 1] = ans[i] / 10;
ans[i] %= 10;
ans[0] = i;
}
len = ans[0];
}
void gc(int b)//高精除
{
bool shit = 0;
for (int i = len;i >= 2;i--)
{
ans[i - 1] += (ans[i] % b) * 10;
ans[i] /= b;
if (!shit)
{
if (!ans[i])len--;
else shit = 1;
}
}
ans[1] /= b;
ans[0] = len;
}
void ggc(int b)//高精除
{
bool shit = 0;
for (int i = tt[0];i >= 2;i--)
{
tt[i - 1] += (tt[i] % b) * 10;
tt[i] /= b;
if (!shit)
{
if (!tt[i])tt[0]--;
else shit = 1;
}
}
tt[1] /= b;
}
struct person//每个人的信息
{
int a, b, c;
}p[100005];
bool cmp(person x, person y)//判断优劣
{
return x.c<y.c;
}
{
maxans[0] = 1;
maxans[1] = -1;
ans[1] = p[0].a;
ans[0] = len = 1;
}
void gcmp(int a[], int b[])
{
if (a[0]>b[0])
{
for (int i = a[0];i >= 0;i--)
maxans[i] = a[i];
return;
}
else if (a[0]<b[0])return;
else
{
for (int j = ans[0];j >= 1;j--)
{
if (a[j]>b[j])
{
for (int i = a[0];i >= 0;i--)
maxans[i] = a[i];
return;
}
else if (a[j]<b[j])return;
}
}
return;
}
int main()
{
scanf("%d", &n);
for (int i = 0;i <= n;i++)
{
scanf("%d%d", &p[i].a, &p[i].b);
p[i].c = p[i].a*p[i].b;
}
sort(p + 1, p + 1 + n, cmp);
//    for(int i=1;i<=n;i++)
//    printf("%d %d %d\n",p[i].a,p[i].b,p[i].c);
for (int i = 1;i <= n;i++)
{
for (int j = tt[0];j >= 0;j--) tt[j] = 0;
for (int j = 0;j <= ans[0];j++) tt[j] = ans[j];
ggc(p[i].b);
gcmp(tt, maxans);
gx(p[i].a);
}
for (int i = maxans[0];i >= 1;i--) printf("%d", maxans[i]);
return 0;
}

posted @ 2016-08-01 17:04 zbtrs 阅读(...) 评论(...) 编辑 收藏