POJ-1405 Heritage-高精度加減乘
Heritage
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7370 | Accepted: 2743 |
Description
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.
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.
Input
The only line of input contains the single integer N (1 <= N <= 18).
Output
Output the numbers that the blanks need to be filled (on separate lines), so that the heritage left for the church is minimized.
Sample Input
2
Sample Output
2 3
Source
ural 1108
根據題目意思就是說,後一個人的分母N是前一個人的分母M進行M*(M-1)+1的計算所得。
然後用高精度加減乘就可以算出來~
根據題目意思就是說,後一個人的分母N是前一個人的分母M進行M*(M-1)+1的計算所得。
然後用高精度加減乘就可以算出來~
#include<stdio.h>
#include<stdlib.h>
#define inf 1000000000
#define maxn 100000
int len1, len2, len3, len[18];
long long a[maxn], b[maxn], ans[maxn], res[18][maxn];
void setAns()
{
for (int i = 0; i < len3; i++)
{
ans[i] = 0;
}
}
void copy()
{
for (int i = 0; i < len3; i++)
{
a[i] = ans[i];
}
len1 = len3;
}
void multiply()
{
int i, j;
setAns();
for (i = 0; i < len2; i++)
{
for (j = 0; j < len1; j++)
{
ans[i+j] += a[j] * b[i];
ans[i+j+1] += ans[i+j] / inf;
if (ans[i+j+1] && len3 <= i + j + 1)
{
len3 = i + j + 2;
}
else if (len3 <= i + j)
{
len3 = i + j + 1;
}
ans[i+j] %= inf;
}
}
}
void plus()
{
int r;
for (len2 = 0, r = 1; len2 < len1; len2++)
{
r += a[len2];
if (r >= inf)
{
b[len2] = r - inf;
r = 1;
}
else
{
b[len2] = r;
r = 0;
}
}
if (r)
{
b[len2++] = r;
}
}
int main()
{
int i, j, n;
scanf("%d", &n);
a[0] = 2;
len1 = 1;
printf("2\n");
for (i = 2; i <= n; i++)
{
plus();
printf("%lld", b[len2-1]);
for (j = len2 - 2; j >= 0; j--)
{
printf("%.9lld", b[j]);
}printf("\n");
if (i < n)
{
multiply();
copy();
}
}
return 0;
}
相關文章
- JavaScript加減乘數運算JavaScript
- 關於javascript中對浮點加,減,乘,除的精度分析JavaScript
- 【實驗】【總結】Oracle日期類操作(格式 加減乘 取毫秒)Oracle
- 用單連結串列實現多項式加,減,乘,簡單微分
- 高精度減法(C語言實現)C語言
- golang 時間加減Golang
- java時間加減Java
- 自定義View加減View
- ORACLE時間加減Oracle
- 【oracle】日期加減計算Oracle
- ACM 分數加減法ACM
- 7.實現加減
- SQL中時間的加減法SQL
- JS 加減乘除 尤其是減法精度問題JS
- 請寫一個整數計算器,支援加減乘三種運算和括號。Python版本Python
- MongoDB 中的【加減乘除】運算MongoDB
- JS加減乘除位移方法封裝JS封裝
- 【jquery】實現購物車加減jQuery
- 加減密和數字簽名
- golang time 時間的加減法Golang
- 11.強化加減按鈕
- JSF的加減法與SeamJS
- 簡單的加減乘除(遞迴)遞迴
- Struts框架 實現複數加減操作框架
- 不用加減乘除做加法(Java實現)Java
- 大資料的運算加減乘除大資料
- timestamp型別的加減運算型別
- 矩陣類及其常規運算(加、減、乘、轉置、求逆、行列式、代數餘子式、伴隨矩陣)矩陣
- mysql查詢中時間、日期加減計算MySql
- 超大整數的加減乘除計算方法
- Verilog實現加減乘除運算
- 演算法搜狗筆試題加減法演算法筆試
- Objective-C 中不帶加減號的方法Object
- JavaScript浮點數加減乘除精確計算JavaScript
- 計算機組成原理浮點數加減計算機
- 7-2 算術入門之加減乘除
- 33:計算分數加減表示式的值
- C語言中的指標加減偏移量C語言指標