SGU 116

简单DP,可以理解为背包问题的变式。(想当初苯渣会错题意,以为只要输出任意一组解啊!结果一趟DFS在第14个点上WA三次啊!TUT)

(第14个点上WA的一定是用贪心或一趟DFS做的!)

首先找到10000以内的super_prime,共201个。(不是打表- -)

这部分代码可以在行到行找到。

然后DP:f[i]代表用最少SUPER_PRIME构成i的数目,prev[i]保存序列。

f[i]=f[i-super[j]]+1(如果super[j]<=i,1<=j<=201)

#include "stdio.h"
#include "string.h"
int main(){
	int f[10002],prev[10002],i,j,N,min,cnt,dcnt;
	int is_prime[10001],super[205],tmp,stack[4000],top;
	scanf("%d",&N);
	memset(is_prime,1,sizeof(is_prime));
	memset(f,0,sizeof(f));
	memset(prev,0,sizeof(f));
	is_prime[0]=is_prime[1]=0;
	for(i=2;i<=10000;i++){
		if(is_prime[i]){
			for(j=2;j<=10000/i;j++){
				is_prime[i*j]=0;
			}
		}
	}
	for(i=2;i<=10000;i++){
		if(is_prime[i]){
			cnt++;
			if(is_prime[cnt]){
				dcnt++;
				super[dcnt]=i;
			}
		}
	}
	f[1]=f[2]=9999999;
	for(i=3;i<=N;i++){
	min=9999999;
		for(j=201;j>=1;j--){
			if(super[j]>i)continue;
			tmp=f[i-super[j]]+1;
			if(tmp<min){
				min=tmp;
				prev[i]=super[j];
				//printf("Prev[i] Become Super[%d]=%d.\n",j,super[j]);
			}
		}
	f[i]=min;
	//printf("F[%d]=%d.\n",i,min);
	}
	if(f[N]!=9999999)printf("%d\n",f[N]);else printf("0");
	top=0;i=N;
	while(prev[i]){
		top++;
		stack[top]=prev[i];
		printf("%d ",prev[i]);
		i-=prev[i];
	}
	return 0;
}

类似的题目还有,SGU 447的optimal rest。

SGU 116

时间: 2024-10-12 20:32:13

SGU 116的相关文章

SGU 116 Index of super-prime 数论+完全背包+输出方案

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=116 题意好晦涩 给你一个不超过一万的数 问它最少可以用多少个“超级素数”来表示 使“超级素数”之和等于它 如果无法这样表示 输出0 否则 按非降序形式输出方案 数论部分就模板的问题 没什么说的 完全背包方面也很常规 说说[输出方案] 背包九讲的伪码给的是二维dp[]的方法 实际上稍加改动就可以用在一维数组上 用一个rec[]记录dp[]的当前状态是从哪个状态转移而来(即上一个状态) 通过

Index of super-prime - SGU 116(素数+背包)

题目大意:素数表2,3,5,7,11.....如果一个素数所在的位置还是素数,那么这个素数就是超级素数,比如3在第2位置,那么3就是超级素数.....现在给你一个数,求出来这个数由最少的超级素数的和组成,输出这个超级素数. 分析:因为给的数字并不大,所以直接用完全背包求出来即可. 代码如下: =======================================================================================================

SGU[116] Index of super-prime

Description 描述 Let P1, P2, … ,PN, … be a sequence of prime numbers. Super-prime number is such a prime number that its current number in prime numbers sequence is a prime number too. For example, 3 is a super-prime number, but 7 is not. Index of supe

SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流

The Glorious Karlutka River =) Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 438 Appoint description: Description A group of Mtourists are walking along the Karlutka river. They want to cross

sgu Kalevich Strikes Back

这道题就是求一个大矩形被n个矩形划分成n+1个部分的面积,这些矩形之间不会相交,可能包含.. 1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #define maxn 120100 6 using namespace std; 7 8 long long s[maxn]; 9 vector<int>g[maxn]; 10 i

数学计数原理(P&#243;lya,高精度):SGU 294 He&#39;s Circles

He's Circles He wrote n letters "X" and "E" in a circle. He thought that there were 2n possibilities to do it, because each letter may be either "X" or "E". But Qc noticed that some different sequences of letters ca

sgu Ice-cream Tycoon

题意:供应商提供n块价格为c的冰淇淋,一个学生想买n块冰淇淋,手中的钱数总共有t元,为了不让买n块冰淇淋所花费的钱数不超过t元,先尽可能卖给这个学生便宜的冰淇淋. 如果这个学生不能买到所需要的冰淇淋则输出“UNHAPPY”,能则输出“HAPPY”. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 200000 5 using namespace std; 6 7

sgu 463 - Walking around Berhattan

K - Walking around Berhattan Time Limit:250MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 463 Description As you probably know, Berhattan is a district of Berland's largest city and it consists of equal squar

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor