【HDOJ】4345 Permutation
即求P1^n1+P2^n2 + ... + Pk^nk <= n,其中Pk为素数的所有可能组合。
思路是DP。1~1000的素数就不到200个。
dp[i][j]表示上式和不超过且当前最小素数为P[j]的所有可能情况。注意dp[i][0]+1即为所求。
/* 4345 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
bool isPrime[maxn];
int P[maxn], pn = ;
__int64 dp[maxn][];
__int64 ans[maxn]; void init() {
memset(isPrime, true, sizeof(isPrime));
rep(i, , maxn) {
if (isPrime[i]) {
P[pn++] = i;
for (int j=i*i; j<maxn; j+=i)
isPrime[j] = false;
}
} #ifndef ONLINE_JUDGE
printf("pn = %d\n", pn);
#endif
ans[] = ;
rep(i, , ) {
rep(j, , pn) {
if (P[j] > i)
break;
for (int k=P[j]; k<=i; k*=P[j]) {
dp[i][j] += dp[i-k][j+] + ;
}
}
per(j, , )
dp[i][j] += dp[i][j+];
ans[i] = dp[i][] + ;
} #ifndef ONLINE_JUDGE
rep(i, , )
printf("%d: %I64d\n", i, ans[i]);
#endif
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n; init(); while (scanf("%d", &n)!=EOF)
printf("%I64d\n", ans[n]); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】4345 Permutation的更多相关文章
- 【题解】CF359B Permutation
[题解]CF359B Permutation 求一个长度为\(2n\)的序列,满足\(\Sigma |a_{2i}-a_{2i-1}|-|\Sigma a_{2i}-a_{2i-1}|=2k\) 这种 ...
- 【HDOJ】4985 Little Pony and Permutation
水题. #include <cstdio> #define MAXN 100005 int buf[MAXN], n; int main() { int i, j, k; while (s ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【leetcode】Next Permutation
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
随机推荐
- php header函数要点
发布:snowfly 来源:网络 [大 中 小] 相信很多人写程序时,使用 header(location) 进行跳转往往不记得写 exit() 语句,这种做法存在严重风险. 从浏览器来看 ...
- oracle通过透明网关连接mysql的配置
之前配置过连接TD的,这一篇是介绍连接Mysql的配置很详细. http://blog.itpub.net/12679300/viewspace-1177222/
- db2使用Java存储过程实现MD5函数
1.数据库版本 2.Java脚本 import java.security.MessageDigest; import COM.ibm.db2.app.UDF; public class MD5UDF ...
- 十、mysql事务的简介
1. myisam跟memory支持表级别锁定 BDB 页级锁定 Innodb 行级锁定 2.表锁(不是表嫂哈) lock table read //只读表锁,也就是说执行了这个锁后,锁内的操作只能为 ...
- easy ui 表单元素input控件后面加说明(红色)
<%-- 上传图片到图库基本信息且将图片关联到图集 开始--%> <div id="win_AddPicLib" class="easyui-windo ...
- UIImagePickerController拍照与摄像(转)
转载自:http://blog.sina.com.cn/s/blog_68edaff101019ppe.html (2012-11-23 14:38:40) 标签: ios iphone 拍照 摄像 ...
- Python的注释
任何时候,我们都可以给程序加上注释.注释是用来说明代码的,给自己或别人看,而程序运行的时候,Python解释器会直接忽略掉注释,所以,有没有注释不影响程序的执行结果,但是影响到别人能不能看懂你的代码. ...
- 扩展ExtJs 4.2.1 htmleditor 添加图片功能
做项目的时候遇到这样一个问题,因为我是用ExtJs做的后台管理框架,所以当我使用ExtJs htmleditor 控件 的时候,发现没有图片上传的功能,于是我打算在网上找找有关的文章,居然真有人写过, ...
- 使用Yeoman搭建 AngularJS 应用 (9) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/install-packages.html 列出当前程序包 我们现在查看一下我们已经安装的程序包,输入下面的命令 bower list 查找 ...
- Matlab中bsxfun和unique函数解析
一.问题来源 来自于一份LSH代码,记录下来. 二.函数解析 2.1 bsxfun bsxfun是一个matlab自版本R2007a来就提供的一个函数,作用是”applies an element-b ...