原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1425

  DP题。

  f[i][j]表示当前数字为i,分解式中最大质数为j的方案数,那么,状态转移方程为:

            f[i][j] = sum(f[i-j][k])

  其中,k为小于等于j的所有质数。

  求具体分解式时可以先转化为从小到大(如样例的“顺数第2”可以表述为“倒数第3”),更容易递推编程。

#include <stdio.h>
#include <string.h> #define N 205
int n, k; int f[N][N]; int prime[N], cnt;
bool isprime[N]; void get_prime()
{
cnt = ;
memset(isprime, , sizeof isprime);
for(int i = ; i < N; i++)
{
if(!isprime[i])
{
prime[cnt++] = i;
for(int j = i*; j < N; j += i)
isprime[j] = true;
}
}
} int main()
{
get_prime();
while(scanf("%d%d", &n, &k) != EOF)
{
memset(f, , sizeof f);
f[][] = ;
for(int i = ; i <= n; i++)
{
for(int j = ; j < cnt; j++)
{
if(prime[j] > i) break;
for(int t = ; t <= prime[j]; t++)
f[i][prime[j]] += f[i-prime[j]][t];
}
}
int sum = ;
for(int i = ; i <= n; i++)
sum += f[n][i];
if(k > sum) k = sum;
printf("%d\n%d=", sum, n);
k = sum - k;
bool flag = false;
do
{
int cnt = ;
int i;
for(i = ; i <= n; i++)
{
if(cnt + f[n][i] > k)
break;
cnt += f[n][i];
}
n -= i;
k = k - cnt;
if(flag) printf("+");
flag = true;
printf("%d", i);
}
while(n != );
printf("\n");
}
return ;
}

CSU 1425 Prime Summation的更多相关文章

  1. CSU 1425 NUDT校赛 I题 Prime Summation

    这个题本来有希望在比赛里面出了的 当时也想着用递推 因为后面的数明显是由前面的推过来的 但是在计算的时候 因为判重的问题 ...很无语.我打算用一个tot[i]来存i的总种树,tot[i]+=tot[ ...

  2. csu 1756: Prime

    1756: Prime Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 128 Mb     Submitted: 281    ...

  3. 【CSU 1756】Prime

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1756 直接暴力O(n^2logn)过不了 两两算gcd 考虑每个数的范围[1,1000]统计一下即 ...

  4. CSU 2018年12月月赛 F(2218): Finding prime numbers

    Description xrdog has a number set. There are 95 numbers in this set. They all have something in com ...

  5. Summation of Four Primes - PC110705

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10168.html 原创:Summ ...

  6. UVA 10168 Summation of Four Primes(数论)

    Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler p ...

  7. CSU 1552 Friends(二分图 + 米勒测试)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1552 Description On an alien planet, every e ...

  8. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  9. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

随机推荐

  1. 【bzoj3796】Mushroom追妹纸

    Portal -->bzoj3796 Description 给出字符串s1.s2.s3,找出一个字符串w,满足: 1.w是s1的子串: 2.w是s2的子串: 3.s3不是w的子串. ​ 求w的 ...

  2. Hdu2433 Travel

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  3. 洛谷P1434 滑雪

    题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最长 ...

  4. jsp 基本原理

    jsp 的本质是 servlet,当用户请求 servlet 的时候,servlet 利用输出流动态输出 HTML 内容. 由于包括了大量的 HTML 标签.大量的静态文本等,导致 servlet 开 ...

  5. Linux系统之路——如何在CentOS7.2安装R和RStudio(Server)

    使用ubuntu的小伙伴们直接使用命令sudo apt-get install r-base-dev或者r-base搞定.然而对于使用centos的我却一直卡在安装这一步,十分的悲催,只有羡慕的份,但 ...

  6. 设置texture

    //获取内部资源贴图 public void setInsideTexture() { Texture2D texture = Resources.Load(texture_url) as Textu ...

  7. Creating a Cron Job in K8S

    Creating a Cron Job Cron jobs require a config file. This example cron job config .spec file prints ...

  8. OpenCV---人脸检测

    一:相关依赖文件下载 https://github.com/opencv/opencv   二:实现步骤(图片检测) (一)读取图片 image= cv.imread("./d.png&qu ...

  9. uva 1636 Headshot

    https://vjudge.net/problem/UVA-1636 首先在手枪里随机装一些子弹,然后抠了一枪,发现没有子弹.你希望下一枪也没有子弹,是应该直接再抠一枪(输出SHOOT)呢,还是随机 ...

  10. 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP

    [题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...