您的位置:首页 > 其它

ural1108Heritage

2017-06-25 13:19 190 查看
Your rich uncle died recently, and the heritage needs to be divided among your relatives and the church (your uncle insisted in his will that the church must get something). There are N relatives (N ≤ 18) that were mentioned in the will. They are sorted in descending order according to their importance (the first one is the most important). Since you are the computer scientist in the family, your relatives asked you to help them. They need help, because there are some blanks in the will left to be filled. Here is how the will looks:

Relative #1 will get 1/… of the whole heritage,

Relative #2 will get 1/… of the whole heritage,



Relative #N will get 1/… of the whole heritage.

The logical desire of the relatives is to fill the blanks in such way that the uncle’s will is preserved (i.e the fractions are non-ascending and the church gets something) and the amount of heritage left for the church is minimized.、

题意:

把1分成1/a1,1/a2…..1/an 其中a1

tip:

考虑1/n - 1/(n+1) = 1/(n*(n+1) ) 所以一个数可以这样分下去,如果剩下的部分是1/n

如果要拆成1/n=1/m+1/r,r>=m>n 只有m=n+1的时候,r最大

用到了大数乘法,把8位缩成一位,相当于写成1e8进制数。。。除了mod的时候变化,其他没什么区别,输出时候注意补0

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL;
int n;
const int maxn = 1e4+1000;
const LL MOD = 100000000;
struct Bigint{
int lena,lens,k,len;
LL a[maxn];
void clear(){len = 0;memset(a,0,sizeof(a));};
void read(char s[]){
clear();
lens = strlen(s) - 1;
len = lena = strlen(s)/8;k = strlen(s) % 8;
if(k != 0){
LL ans = 0;
for(int i = 0 ; i < k ; i++)
ans = ans*10+s[i]-'0';
a[len+1] = ans;
len++;
}
int j = k;
for(int i = lena; i >= 1 ; i--){
LL ans = 0;
for(int cnt = 0 ; cnt < 9 ; cnt++){
ans = ans*10+s[++j]-'0';
}
a[i] = ans;
}
}
void output(){
for(int i = len ; i >= 1 ; i--){
if(i != len)  printf("%08lld",a[i]);
else    printf("%lld",a[i]);
}
if(len == 0)    printf("0");
printf("\n");
}
};
Bigint init,pre;
Bigint MINUS(Bigint A,Bigint B){
Bigint C = A;
for(int i = 1; i <= C.len ; i++){
C.a[i] -= B.a[i];
while(C.a[i] < 0){C.a[i] += MOD;C.a[i+1]--;}
}
while(C.len > 0 && C.a[C.len] == 0) C.len--;
//    cout <<" MINUS : "<<endl;
//    C.output();
return C;
}
Bigint PLUS(Bigint A,Bigint B){
Bigint C = A;C.len = max(C.len,B.len)+1;
for(int i = 1; i <= B.len ; i++)    C.a[i] += B.a[i];
for(int i = 1; i < C.len ; i++) C.a[i+1] += C.a[i]/MOD,C.a[i] %= MOD;
while(C.len > 0 &&C.a[C.len] == 0)  C.len--;
//    cout <<"plus : "<<endl;
//    C.output();
return C;
}
Bigint MUL(Bigint A,Bigint B){
Bigint C;C.clear();C.len = A.len+B.len;
for(int i = 1; i <= A.len ;i++){
for(int j = 1; j <= B.len ; j++){
C.a[i+j-1] += A.a[i] * B.a[j];
}
}
for(int i = 1; i < C.len ; i++){
C.a[i+1] += C.a[i]/MOD;C.a[i] %= MOD;
}
while(C.len > 0 && C.a[C.len] == 0) C.len--;
//    cout <<" MUL : "<<endl;
//    C.output();
return C;
}
char s[2];
void sov(){
s[0] = '1';s[1] = '\0';init.read(s);
s[0] = '2';s[1] = '\0';pre.read(s);
printf("2\n");
for(int i = 1; i < n ; i++){
//init.output();
pre = PLUS(MUL(MINUS(pre,init),pre),init);
pre.output();
}
}
int main(){
while(scanf("%d",&n) == 1){
sov();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: