母函数刷题

Ignatius and the Princess III

题目描述

"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.

"The second problem is, given an positive integer N, we define an equation like this:
  N=a[1]+a[2]+a[3]+...+a[m];
  a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
  4 = 4;
  4 = 3 + 1;
  4 = 2 + 2;
  4 = 2 + 1 + 1;
  4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"

输入

The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.

输出

For each test case, you have to output a line contains an integer P which indicate the different equations you have found.

样例输入 Copy
4
10
20
样例输出 Copy
5
42
627
#include<bits/stdc++.h>
using namespace std;
int a[20000];
int ans[20000];
int main()
{
    int n;
    while(cin>>n){
        if(!n)return 0;
        for (int i = 0; i <=n; i++)
        {
            a[i]=1;
            ans[i]=0;
        }
        for(int i=2;i<=n;i++){
            for(int j=0;j<=n;j++){
                for(int k=0;k<=n-j;k+=i){
                    ans[j+k]+=a[j];
                }
            }
            for (int j = 0; j <=n; j++)
            {
                a[j]=ans[j];
                ans[j]=0;
            }
        }
        cout<<a[n]<<'\n';
    }
}

Square Coins

题目描述

People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 289-credit coins, are available in Silverland.
There are four combinations of coins to pay ten credits:

ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin.

Your mission is to count the number of ways to pay a given amount using coins of Silverland.

输入

The input consists of lines each containing an integer meaning an amount to be paid, followed by a line containing a zero. You may assume that all the amounts are positive and less than 300.

输出

For each of the given amount, one line containing a single integer representing the number of combinations of coins should be output. No other characters should appear in the output.

样例输入 Copy
2
10
30
0
样例输出 Copy
1
4
27
#include<bits/stdc++.h>
using namespace std;
int coin[18];
int a[10000];
int ans[10000];
int main()
{
    int n;
    for(int i=1;i<=17;i++)coin[i]=pow(i,2);
    while(cin>>n){
        if(!n)return 0;
        for (int i = 0; i <=n; i++){
            a[i]=1;
            ans[i]=0;
        }
        for(int i=2;i<=17;i++){
            for(int j=0;j<=n;j++)
                for(int k=0;k<=n-j;k+=coin[i])
                    ans[j+k]+=a[j];
            for(int j=0;j<=n;j++){
                a[j]=ans[j];
                ans[j]=0;
            }
        }
        cout<<a[n]<<'\n';
    }
}

Holding Bin-Laden Captive!

题目描述

We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China!
“Oh, God! How terrible! ”

Don’t be so afraid, guys. Although he hides in a cave of Hang Zhou, he dares not to go out. Laden is so bored recent years that he fling himself into some math problems, and he said that if anyone can solve his problem, he will give himself up!
Ha-ha! Obviously, Laden is too proud of his intelligence! But, what is his problem?
“Given some Chinese Coins (硬币) (three kinds-- 1, 2, 5), and their number is num_1, num_2 and num_5 respectively, please output the minimum value that you cannot pay with given coins.”
You, super ACMer, should solve the problem easily, and don’t forget to take $25000000 from Bush!

输入

Input contains multiple test cases. Each test case contains 3 positive integers num_1, num_2 and num_5 (0<=num_i<=1000). A test case containing 0 0 0 terminates the input and this test case is not to be processed.

输出

Output the minimum positive value that one cannot pay with given coins, one line for one case.

样例输入 Copy
1 1 3
0 0 0
样例输出 Copy
4
#include<bits/stdc++.h>
using namespace std;
int a[100001];
int ans[100001];
int main()
{
    int s,b,c;
    while(cin>>s>>b>>c){
        if(s==0&&b==0&&c==0)return 0;
        int sum=s+b*2+c*5;
        for(int i=0;i<=sum;i++){
            a[i]=1;
            ans[i]=0;
        }
        for(int i=0;i<=s;i++)
            for(int j=0;j<=2*b;j+=2)ans[i+j]+=a[i];
        for(int i=0;i<=sum;i++){a[i]=ans[i];ans[i]=0;};
        for(int i=0;i<=s+2*b;i++)
            for(int j=0;j<=5*c;j+=5)ans[i+j]+=a[i];
        int p=1;
        for(int i=0;i<=sum;i++)if(!ans[i]){cout<<i<<'\n';p=0;break;}
        if(p)cout<<sum+1<<'\n';
    }
    return 0;
}

Big Event in HDU

题目描述

Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).

输入

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.

输出

For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.

样例输入 Copy
2
10 1
20 1
3
10 1 
20 2
30 1
-1
样例输出 Copy
20 10
40 40
#include<bits/stdc++.h>
using namespace std;
const int N=51;
const int M=101;
int v[N];
int num[N];
int a[N*N*M];
int ans[N*N*M];
int main()
{
    int n,sum;
    while(cin>>n){
        if(n<0)return 0;
        sum=0;
        for(int i=1;i<=n;i++){
            cin>>v[i];
            cin>>num[i];
            sum+=v[i]*num[i];
        }
        memset(a,0,sizeof(a));
        memset(ans,0,sizeof(ans));
        for(int i=0;i<=num[1];i++)a[i*v[i]]=1;
        for(int i=2;i<=n;i++){
            for(int j=0;j<=sum;j++){
                for(int k=0;k<=num[i]&&(j+k*v[i])<=sum;k++){
                    ans[j+k*v[i]]+=a[j];
                }
            }
            for(int j=0;j<=sum;j++){
                a[j]=ans[j];
                ans[j]=0;
            }
        }
        for(int i=sum/2;i>=0;i--){
            if(a[i]){
                cout<<sum-i<<' '<<i<<'\n';
                break;
            }
        }
    }
}

The Balance

题目描述

Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.

输入

The input consists of multiple test cases, and each case begins with a single positive integer N (1<=N<=100) on a line by itself indicating the number of weights you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100.

输出

For each input set, you should first print a line specifying the number of qualities which cannot be measured. Then print another line which consists all the irrealizable qualities if the number is not zero.

样例输入 Copy
3
1 2 4
3
9 2 1
样例输出 Copy
0
2
4 5
#include<bits/stdc++.h>
using namespace std;
int num[101];
int ans[10001];
int a[10001];
int main()
{
    int n,sum,p;
    while(cin>>n){
        sum=0;
        p=0;
        for(int i=1;i<=n;i++){
            cin>>num[i];
            sum+=num[i];
        }
        memset(ans,0,sizeof(ans));
        memset(a,0,sizeof(a));
        a[num[1]]=1,a[0]=1;
        for(int i=2;i<=n;i++){
            for(int j=0;j<=sum;j++){
                ans[j+num[i]]+=a[j];
                ans[j]+=a[j];
                ans[abs(j-num[i])]+=a[j];
            }
            for(int j=0;j<=sum;j++){
                a[j]=ans[j];
                ans[j]=0;
            }
        }
        for(int i=0;i<=sum;i++)
            if(!a[i])p++;
        cout<<p<<'\n';
        for(int i=0;i<=sum;i++)
            if(!a[i])cout<<i<<' ';
    }
}

 Fruit

题目描述

转眼到了收获的季节,由于有TT的专业指导,Lele获得了大丰收。特别是水果,Lele一共种了N种水果,有苹果,梨子,香蕉,西瓜……不但味道好吃,样子更是好看。

于是,很多人们慕名而来,找Lele买水果。

甚至连大名鼎鼎的HDU ACM总教头 lcy 也来了。lcy抛出一打百元大钞,"我要买由M个水果组成的水果拼盘,不过我有个小小的要求,对于每种水果,个数上我有限制,既不能少于某个特定值,也不能大于某个特定值。而且我不要两份一样的拼盘。你随意搭配,你能组出多少种不同的方案,我就买多少份!"

现在就请你帮帮Lele,帮他算一算到底能够卖出多少份水果拼盘给lcy了。

注意,水果是以个为基本单位,不能够再分。对于两种方案,如果各种水果的数目都相同,则认为这两种方案是相同的。

最终Lele拿了这笔钱,又可以继续他的学业了~

输入

本题目包含多组测试,请处理到文件结束(EOF)。
每组测试第一行包括两个正整数N和M(含义见题目描述,0<N,M<=100)
接下来有N行水果的信息,每行两个整数A,B(0<=A<=B<=100),表示至少要买该水果A个,至多只能买该水果B个。

输出

对于每组测试,在一行里输出总共能够卖的方案数。
题目数据保证这个答案小于10^9

样例输入 Copy
2 3
1 2
1 2
3 5
0 3
0 3
0 3
样例输出 Copy
2
12
#include<bits/stdc++.h>
using namespace std;
const int N=101;
int s[N],d[N];
int ans[N];
int a[N];
int main()
{
    int n,m;
    while(cin>>n>>m){
        for(int i=1;i<=n;i++){
            cin>>s[i]>>d[i];
        }
        memset(ans,0,sizeof(ans));
        memset(a,0,sizeof(a));
        for(int i=s[1];i<=d[1];i++)a[i]=1;
        for(int i=2;i<=n;i++){
            for(int j=0;j<=m;j++){
                for(int k=s[i];k<=d[i]&&j+k<=m;k++){
                    ans[j+k]+=a[j];
                }
            }
            for(int j=0;j<=m;j++){
                a[j]=ans[j];
                ans[j]=0;
            }
        }
        cout<<a[m]<<'\n';
    }
}

排列组合

题目描述

有n种物品,并且知道每种物品的数量。要求从中选出m件物品的排列数。例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB","BA"两种。

输入

每组输入数据有两行,第一行是二个数n,m(1<=m,n<=10),表示物品数,第二行有n个数,分别表示这n件物品的数量。

输出

对应每组数据输出排列数。(任何运算不会超出2^31的范围)

样例输入 Copy
2 2
1 1
样例输出 Copy
2
#include<bits/stdc++.h>
using namespace std;
int a[11];
int jie[11];
double ans[11];
double b[11];
int main()
{
    int n,m;
    jie[0]=1;
    for(int i=1;i<=10;i++)jie[i]=jie[i-1]*i;
    while(cin>>n>>m){
        for(int i=1;i<=n;i++)cin>>a[i];
        memset(ans,0,sizeof(ans));
        memset(b,0,sizeof(b));
        for(int i=0;i<=a[1];i++){
            b[i]=1.0/jie[i];
        }
        for(int i=2;i<=n;i++){
            for(int j=0;j<=m;j++){
                for(int k=0;k<=m-j&&k<=a[i];k++){
                    ans[j+k]+=(1.0*b[j]/jie[k]);
                }
            }
            for(int j=0;j<=m;j++){
                b[j]=ans[j];
                ans[j]=0;
            }
        }
        printf("%.0f\n",b[m]*jie[m]);
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值