acdream 1431 Sum vs Product
2014-10-06 18:26
417 查看
Sum vs Product
Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)SubmitStatisticNext
Problem
Problem Description
Peter has just learned mathematics. He learned how to add, and how to multiply. The fact that 2 + 2 = 2 × 2 has amazed him greatly. Now he wants find more such examples. Peters calls a collection of numbersbeautiful if the product of the numbers in it is equal to their sum.
For example, the collections {2, 2}, {5}, {1, 2, 3} are beautiful, but {2, 3} is not.
Given n, Peter wants to find the number of beautiful collections with n numbers. Help him!
Input
The first line of the input file contains n (2 ≤ n ≤ 500)
Output
Output one number — the number of the beautiful collections with n numbers.
Sample Input
2 5
Sample Output
1 3
Hint
The collections in the last example are: {1, 1, 1, 2, 5}, {1, 1, 1, 3, 3} and {1, 1, 2, 2, 2}.
Source
Andrew Stankevich Contest 23
Manager
mathlover题解及代码:
通过打表前几项我们会发现构成n,比如n=5时,其形式之一是1 1 2 2 2,都是这种很多1,然后其他数字组合的形式。那么我们就可以枚举除了1以外的数字的组合,来计算sum
。比如数字组合为2 3 4,那么根据公式我们知道2*3*4=24,2+3+4=9,那么我们还需要补上15个1,加上2 3 4 这三个数字,总共是18个数字,那么2 3 4必然属于sum[18]里面的一中情况。得到验证,这样我们就能用dfs来求出所有的情况数了。
下面的代码是dfs的代码,因为怕超时的缘故,题目AC的代码是打表之后交的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int sum[510];
void init()
{
memset(sum,0,sizeof(sum));
}
void dfs(int nt,int nu,int su,int k)
{
for(int i=k;i<=500;i++)
{
if(nu*i>1000) break;
sum[nu*i-su-i+nt+1]++;
//printf("%d %d %d %d %d\n",nu,su,i,nt+1,nu*i-su-i+nt+1);
dfs(nt+1,nu*i,su+i,i);
}
}
int main()
{
init();
for(int i=2;i<=500;i++)
dfs(1,i,i,i);
for(int i=2;i<=500;i++)
printf("%d,",sum[i]);
return 0;
}
相关文章推荐
- ACdream 1431 Sum vs Product
- Sum vs Product(ACdream) —— dfs
- Acdream 1431 Sum vs Product(dfs+剪枝)
- ACdream 1431 Sum vs Product
- ACdream 1431——Sum vs Product——————【dfs+剪枝】
- ACdream 1431 Sum vs Product
- acdream 1431 Sum vs Product
- ACdream OJ 1154 Lowbit Sum
- ACDream - Sum
- ACdream 1139(Sum-逆元)
- ACdreamOJ 1154 Lowbit Sum (数位dp)
- 数位DP ACdream 1154 Lowbit Sum
- acdream Divide Sum
- ACdream: Sum
- ACdream1431-Sum vs Product
- ACdreamOJ 1154 Lowbit Sum (数字dp)
- 7、步步为营VS 2008 + .NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count、LongCount、Sum、Min、Max、Average、Aggregate、Cast、DefaultIfEmpty、SequenceEqual、OfType、ToArray、ToList、ToDictionary
- ACdream 1015 Double Kings【贪心*2】
- AC日记——最小的N个和 codevs 1245
- ACdream 少了哪个妹子?( 签到)