UVA1258 LA4721 Nowhere Money数学计算
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA1258 LA4721 Nowhere Money数学计算相关的知识,希望对你有一定的参考价值。
In Nowhere town, people use “coin and slot” as their money. There are 2 types of coins called size1 and size2. Size2 coin is twice the thickness of size1. People stack their coins in slots and use them as money. There are many size of slots. The slot of size n is able to stack up n size1 coins. Only filled- up slot is considered as legal money. The value of each filled- up slot is the distinct ways its can stack coins inside. For example, for size- 1 slot, there is only one way to stack a single size1 coin inside. For size- 2 slot, there are 2 ways to stack two size1 coins or one size2 coin inside. And for size- 5 slot, there are 8 ways to stack coins inside, which can be illustrated as follow: 1 1 1 1 1, 1 1 1 2, 1 1 2 1, 1 2 1 1, 2 1 1 1, 1 2 2, 2 1 2, 2 2 1. So the value of filled up slot with size- 1, size- 2 and size- 5 are 1, 2 and 8 (monetary) units respectively (regardless of the type of coins or the ways they are stacked).
Mr. Thinktwice is an owner of a grocery store in this town. He noticed that customers are likely to go to the shop that can return the (money) change in the form that suits their customer. And from his little survey, he found that most customers would like to get their amount of change in the form according to these 2 simple constraints.
- The number of slots is minimum.
- The size of each slot must be different from each other by at least 2. —This means that customers does not want any slots with the same size and it will be easier for them to distinguish these differences if the sizes are not too close.
So Mr. Thinktwice ask you to write a program that can give him a series of slot sizes for a
given amount of change according to the previous constraints. Moreover, the series must be sorted in
descending order. For more specific, any amount of change can be written in this formula.
X = ∑ i = 1 n T ( s i ) = T ( s 1 ) + ⋅ ⋅ ⋅ + T ( s i ) + ⋅ ⋅ ⋅ + T ( s n ) , s 1 ≫ s 2 ≫ ⋅ ⋅ ⋅ ≫ s i ≫ ⋅ ⋅ ⋅ ≫ s n > 0 X = \\sum_{i=1}^nT(s_i)\\\\ = T(s1) + · · · + T(si) + · · · + T(sn), s1 ≫ s2 ≫ · · · ≫ si ≫ · · · ≫ sn > 0 X=∑i=1nT(si)=T(s1)+⋅⋅⋅+T(si)+⋅⋅⋅+T(sn),s1≫s2≫⋅⋅⋅≫si≫⋅⋅⋅≫sn>0
where
X is an amount of change.
n is the total number of slot.
si is the size of i-th slot.
T is a function mapping from a slot size to a number of distinct ways of stacking coins. and when j ≫ k ≡ j ≥ k + 2
For example :
10 = T(5) + T(2)
= 8 + 2
1, 000, 000 = T(29) + T(25) + T(23) + T(11) + T(9)
= 832, 040 + 121, 393 + 46, 368 + 144 + 55
Input
Input is a standard input which contains a set of integer. Each line of the input is an amount of change which represents by a positive integer less than or equal to 5,000,000,000,000,000,000 or 5 × 1018. The input is terminated when the EOF (End- Of- File) is reached.
Output
For each amount of change, generate 4 lines of output data. The first line is the amount of change itself. The second line is a series of slot sizes (in descending order) separated by spaces. (The maximum slot size is less than or equal to 90.) The third line is a series of corresponding slot values. The fourth line is a blank line.
Sample Input
1
10
1000000
Sample Output
1
1
1
10
5 2
8 2
1000000
29 25 23 11 9
832040 121393 46368 144 55
问题链接:UVA1258 LA4721 Nowhere Money
问题简述:(略)
问题分析:数学计算问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的Java语言程序如下:
/* UVA1258 LA4721 Nowhere Money */
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
const int N = 90;
LL a[N + 1], b[N + 1];
int flag[N + 1];
void calc(LL n, int m)
{
if (n == 0 || m < 0)
return;
else if(a[m] <= n) {
flag[m] = 1;
calc(n - a[m], m - 1);
} else
calc(n, m - 1);
}
int main()
{
a[0] = 0;
a[1] = 1;
a[2] = 2;
for (int i = 3; i <= N; i++)
a[i] = a[i - 2] + a[i - 1];
LL n;
while (~scanf("%lld", &n)) {
memset(flag, 0, sizeof flag);
calc(n, N);
int cnt = 0;
for (int i = N; i >= 0; i--)
if (flag[i]) b[cnt++] = i;
printf("%lld\\n", n);
for (int i = 0; i < cnt; i++)
printf("%lld ", b[i]);
printf("\\n");
for (int i = 0; i < cnt; i++)
printf("%lld ", a[b[i]]);
printf("\\n\\n");
}
return 0;
}
以上是关于UVA1258 LA4721 Nowhere Money数学计算的主要内容,如果未能解决你的问题,请参考以下文章
UVA763 LA5339 Fibinary Numbers大数