JOJ 2201 Prime and Decompounding

本文介绍了一种使用完全背包模型来解决质数分解计数问题的方法。通过预先计算2到330之间的所有质数,并利用这些质数作为基本元素进行组合,可以计算出任意给定正整数N的不同质数分解形式的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 A positive interger except 1 can be decompounded to the sum of several prime numbers(include only one number). For exsample, four of the different forms in essence of the decompounding for 9 are: 9 = 2 + 5 + 2 = 2 + 3 + 2 + 2 = 3 + 3 + 3 = 2 + 7. Here the different forms in essence means that one expression can't be the same with others by exchange numbers in the it. Now do you know how many different decompounding forms in essence for any given number N.

Input and Output

The input will consist several lines, each line is a positive interger N(2 < N <= 330) for which output the anwser in a single line.

Sample Input

2
200

Sample Output

1

9845164

 

完全背包的模型,注意细节就行了。。。。。

 

#include <iostream>
#include <cmath>
using namespace std;

#define MAXN 1000000
int hash[1000];   //打表保存2-330之间的素数,方便后面计算、、、、

int f[MAXN];
bool is_prime(int n)
{
 int i;
 for(i = 2; i <= sqrt(n); i++)
 {
  if(n%i==0)
   return false;
 }

 return true;
}


int main()
{
 int i, count = 0, j;
 for(i = 2; i <= 330; i++)
 {
  if(is_prime(i))
  {
   hash[count] = i;
   count++;
  }
 }

/* for(i = 0; i < count; i++)
 {
  cout<<hash[i]<<" ";
 }
 
 cout<<endl;*/
 
 f[0] = 1;    //完全背包的模型、、、、、、
 for(i = 0; i < count; i++)
 {
  for(j = hash[i]; j < 100000; j++)
  {
   f[j] += f[j-hash[i]];
  }
 }


 int n;
 while(cin>>n)
 {
  cout<<f[n]<<endl;
 }


 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值