码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷 [AHOI2001]质数和分解

时间:2016-03-27 11:09:14      阅读:486      评论:0      收藏:0      [点我收藏+]

标签:

 题目描述 Description
任何大于 1 的自然数 n 都可以写成若干个大于等于 2 且小于等于 n 的质
数之和表达式(包括只有一个数构成的和表达式的情况),并且可能有不止一
种质数和的形式。例如,9 的质数和表达式就有四种本质不同的形式:
9 = 2 + 5 + 2 = 2 + 3 + 2 + 2 = 3 + 3 + 3 = 2 + 7 。
这里所谓两个本质相同的表达式是指可以通过交换其中一个表达式中
参加和运算的各个数的位置而直接得到另一个表达式。
试编程求解自然数 n 可以写成多少种本质不同的质数和表达式。
 输入输出格式 Input/output
输入格式:
文件中的每一行存放一个自然数 n(2 < n < 200) 。
输出格式:
依次输出每一个自然数 n 的本质不同的质数和表达式的数目。
 输入输出样例 Sample input/output
样例测试点#1
输入样例: 

2
200

输出样例:

1
9845164

技术分享
#include<iostream>
using namespace std;
#include<cstdio>
#include<cmath>
#include<cstring>
int n,t=0;
int a[201];
bool judge(int k)
{
    bool flag=true;
    for(int i=2;i<=sqrt(k);++i)
    if(k%i==0)
    {
        flag=!flag;
        break;
    }
    return flag;
}
void zhishu()
{
    for(int i=2;i<=200;++i)
    if(judge(i))
    {
        ++t;
        a[t]=i;
    }
}
int f[201];
int main()
{
    
    zhishu();
    while(scanf("%d",&n)==1)
    {
        memset(f,0,sizeof(f));
        f[0]=1;
       for(int i=1;i<=t;++i)//所有的可以用的质数 
         for(int j=a[i];j<=n;++j)
         f[j]+=f[j-a[i]]; /*DP方程的思路:f[j]等于所有可以通过j-一个质数的方案总和*/
         cout<<f[n]<<endl;
   }
    return 0;
}
View Code

 

洛谷 [AHOI2001]质数和分解

标签:

原文地址:http://www.cnblogs.com/c1299401227/p/5325081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!