poj1028 Ignatius and the Princess III  hdu1709The Balance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9592    Accepted Submission(s): 6768


Problem Description
"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!"
 

Input
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.
 

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

Sample Input
       
       
       
       
4 10 20
 

Sample Output
       
       
       
       
5 42 627
 

Author
Ignatius.L
经典的母函数题,详见代码用好这一们模板,其实很多母函数就是在套用而矣!

#include <iostream>
#include <stdio.h>
using namespace std;
int tempresult[121],result[121];
int main()
{
   int n,i,j,k;
   while(scanf("%d",&n)!=EOF)
   {

       for(i=0;i<=n;i++)//初始化
       {
           result[i]=1;//一个式子一个式子的乘积所得到的系数,最开始为(1+x+x^2+x^3...)所以全为1
           tempresult[i]=0;//这个就是在计算中临时保存结果的数列,最终还原给result
       }
       for(i=2;i<=n;i++)//i代表第i个括号里的式子如(1+x^2+x^4...)所对应的i为2以此向后推
       {
            for(j=0;j<=n;j++)//j代表是,每一次算出来的结果式子的系数,这里,因为只求n最大的指数,所以可以把大于n的不看了,因为,
已经大于n了,那么算的结果,对最后的结果,不再产生影响
                for(k=0;k<=n-j;k=k+i)//k表示的是当前要相乘的数列的指数,如第一次要和,(1+x^2+x^3+...),第二次,要和(1+x^3+x^6...),
我们可以看出,k这三个数列,每次指数,加的都是1,2,3,这不就是加的i么,和上面一样,大于n,再算对最后的结果没影响,自然可以不计
                {
                    tempresult[j+k]+=result[j];//新形成的数列的指数为j+k的系数,因为,新乘的项系数都为1,故只用加个result[j],
就可以了
                }
            for(j=0;j<=n;j++)
            {
                result[j]=tempresult[j];//把乘了一项了系数都保存起来
                tempresult[j]=0;//临时保存的没有用了,清0
            }
       }
       printf("%d\n",result[n]);

   }



  return 0;
}
我们再看一个,非常相似的问题


62:12:11
384:00:00
  • Overview
  • Problem
  • Status
  • Rank
A  B  C  D  E  F  G  H  I  J  K  L  M  N  O
A - The Balance
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

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.
 

Input

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.
 

Output

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.
 

Sample Input

             
             
             
             
3 1 2 4 3 9 2 1
 

Sample Output

             
             
             
             
0 2 4 5
在这个问题中,我们有几个重量,我们要求的是,什么不能称出,
其实,这也是可以用母函数做的!
#include<stdio.h>
#include<math.h>
int c2[100000],c1[100000];
int main()
{
	int i,j,k,s,m,x,n,a[105];
	while(scanf("%d",&n)!=EOF)
	
	{
		for(i=0,s=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			s+=a[i];
		}
		for(i=0;i<s;i++)
		{
			c2[i]=0;
		}
		for(i=0,k=0,m=0;i<n;++i) 
		{
			if(c2[a[i]-1]==0)
				{
					c1[m]=a[i];
					c2[a[i]-1]++;
					m++;
				}//printf("%d ",j);
			for(j=0;j<k;++j) 
			{
				x=a[i]+c1[j];
				if(c2[x-1]==0)
				{
					c1[m]=x;
					c2[x-1]++;
					m++;
				}
				x=abs(a[i]-c1[j]);
				if(c2[x-1]==0&&x>0)
				{
					c1[m]=x;
					c2[x-1]++;
					m++;
				}

			}
			k=m;
		}
		//for(i=0;i<k;i++)
			//	printf("%d ",c1[i]);
		printf("%d\n",s-k);
		if(k!=s)
		{
			for(j=0,i=0;i<s;i++)
			{
				if(c2[i]==0)
					if(j!=0)
						printf(" %d",i+1);
					else 	
					{
						j++;
						printf("%d",i+1);
					}
			}
			printf("\n");
		}
	}
	return 0;
}


 
FAQ | About Virtual Judge |  Forum |  Discuss |  Open Source Project
All Copyright Reserved ©2010-2012  HUST ACM/ICPC TEAM 
Anything about the OJ, please ask in the  forum, or contact author: Isun
Server Time: 

 

Statistic |  Submit |  Discuss |  Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2013 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000763(s) query 1, Server time : 2013-07-15 21:54:47, Gzip enabled

你可能感兴趣的:(poj1028 Ignatius and the Princess III  hdu1709The Balance)