标签:输入 换问题 数学 str problem 函数 国家 tle std
Input每行只有一个正整数N,N小于32768。Output对应每个输入,输出兑换方法数。Sample Input
2934 12553
Sample Output
718831 13137761
解法1:母函数
代码如下:
#include<iostream>
using namespace std;
#define n 32770
int main()
{
  int a[32770],b[32770],i,j,k;
  for(i=0;i<=n;i++)
  {
   a[i]=1;
   b[i]=0;
  }
  for(i=2;i<=3;i++)
  {
   for( j=0;j<=n;j++)
   {
    for( k=0;k+j<=n;k+=i)
     b[j+k]+=a[j];
   }
   for(j=0;j<=n;j++)
   {
    a[j]=b[j];
    b[j]=0;
   }
   
  }
  int N;
  while(cin>>N){
  cout<<a[N]<<endl;
 }
 return 0;
}
解法2:完全背包
代码如下:
#include<iostream>
using namespace std;
int main()
{
 int n;
 int i,j;
 int dp[32769];
 memset(dp,0,sizeof(dp));
 dp[0]=1;
 for(i=1;i<=3;i++)
  for(j=i;j<=32769;j++)
   dp[j]+=dp[j-i];
  while(cin>>n)
  {
   
   cout<<dp[n]<<endl;
   
  }
  return 0;
}
解法3:数学方法
代码如下:
#include <iostream>
using namespace std;
int main()
{
int n,sum,i;
while (cin>>n)
{
for (i=0,sum=0;i<=n/3;i++)
sum+=(n-3*i)/2+1;
cout<<sum<<endl;
}
return 0;
}
/*
i 是枚举3分硬币的个数
当有i个3分硬币的时候,2分硬币可以有0,1,2…(n-3*i)/2个,共(n-3*i)/2+1
种,3分硬币和2分硬币个数确定了,1分硬币就确定
*/
标签:输入 换问题 数学 str problem 函数 国家 tle std
原文地址:https://www.cnblogs.com/cstdio1/p/11074630.html