Problem B
Pay the Price
Input: standard input
Output: standard output
Time Limit: 2 seconds
Memory Limit: 32 MB
In ancient days there was a country whose people had very interesting habits. Some of them were lazy, some were very rich, some were very poor and some were miser. Obviously, some of the rich were miser (A poor was never miser as he had little to spend) and lazy but the poor were lazy as well (As the poor were lazy they remained poor forever). The following things were true for that country
a) As the rich were miser, no things price was more than 300 dollars (Yes! their currency was dollar).
b) As all people were lazy, the price of everything was integer (There were no cents and so beggars always earned at least one dollar)
c) The values of the coins were from 1 to 300 dollars, so that the rich (who were idle) could pay any price with a single coin.
Your job is to find out in how many ways one could pay a certain price using a limited number of coins (Note that the number of coins paid is limited but not the value or source. I mean there was infinite number of coins of all values). For example, by using three coins one can pay six dollars in 3 ways, 1+1+4, 1+2+3, and 2+2+2. Similarly, one can pay 6 dollars using 6 coins or less in 11 ways.
Input
The input file contains several lines of input. Each line of input may contain 1, 2 or 3 integers. The first integer is always N (0<=N<=300), the dollar amount to be paid. All other integers are less than 1001 and non-negative.
Output
For each line of input you should output a single integer.
When there is only one integer N as input, you should output in how many ways N dollars can be paid.
When there are two integers N and L1 as input, then you should output in how many ways Ndollars can be paid using L1 or less coins.
When there are three integers N, L1 and L2 as input, then you should output in how many ways N dollars can be paid using L1, L1+1 …, L2 coins (summing all together). Remember that L1 is not greater than L2.
Sample Input
6
6 3
6 2 5
6 1 6
Sample Output
11
7
9
11
题意:
有1到300的价值的硬币,给你一个价值,后面跟着0到2个数字,0个数表示可以用任意的个数凑出这个价值,1个数字a表示用不大于a个硬币去凑,2个数字a,b表示用a到b范围内的硬币去凑。
思路:
还是想的完全背包再加一维表示个数,结果TLE了,无法改进了。
看了题解,要用ferrers图的性质。
性质:
1.一个整数n 拆分成恰好为m 部分的拆分数,等于这个整数n 拆分成最大部分为m 的拆分数.
推论:整数n 拆分成最多有m 部分的拆分数,等于这个整数拆分成每一部分不超过m 的拆分数.
用ferrers的图与其共轭图证明,详情请看百度文库。
2.把整数n 分裂成m 个不同部分的拆分数,等于把整数n - m( m + 1)/2 分裂成最多m 个部分的拆
分数. 这里n >= m( m + 1)/2.
就是先填1,2,3...之后,然后每层加若干个。
这题肯定是要用性质1的推论解决问题了,注意0和-1的区别就ok了,WA了好多次 - -
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 100005 #define mod 1000000007 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 0.000001 typedef long long ll; using namespace std; ll n,m,ans,cnt,tot,flag; ll le,ri; char s[maxn]; ll dp[maxn]; ll solve(ll x) { ll i,j,k,t; if(x==-1) return 0; for(i=0;i<=n;i++) dp[i]=0; dp[0]=1; for(i=1;i<=x;i++) { for(j=i;j<=n;j++) { dp[j]+=dp[j-i]; } } return dp[n]; } int main() { ll i,j,t; while(gets(s)!=NULL) { le=ri=-1; sscanf(s,"%lld %lld %lld",&n,&le,&ri); if(le==-1) le=0,ri=300; else if(ri==-1) { ri=le; le=0; } ri=min(300LL,ri); if(le>ri) { printf("0\n"); continue ; } ans=solve(ri)-solve(le-1); printf("%lld\n",ans); } return 0; }