【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, ...
随机推荐
- Java 多线程 锁 存款 取款
http://jameswxx.iteye.com/blog/806968 最近想将java基础的一些东西都整理整理,写下来,这是对知识的总结,也是一种乐趣.已经拟好了提纲,大概分为这几个主题: ja ...
- PHP curl 参数详解
PHP curl参数详解,分享一下. curl_setopt (PHP 4 >= 4.0.2) curl_setopt -- 为CURL调用设置一个选项 描述 bool curl_setopt ...
- PHP获取搜索引擎关键字来源(百度、谷歌、雅虎、搜狗、搜搜、必应、有道)
<?php //获取来自搜索引擎入站时的关键词 function get_keyword($url,$kw_start) { $start=stripos($url,$kw_start); $u ...
- (转载)MS SQL Server 未公开的加密函数有哪些?
MS SQL Server 未公开的加密函数有哪些? 以下的文章是对MS SQL Server 未公开的加密函数的具体操作,如果你对其相关的实际操作有兴趣的话,你就可以点击了. MS SQL Serv ...
- Spark Streaming揭秘 Day13 数据安全容错(Driver篇)
Spark Streaming揭秘 Day13 数据安全容错(Driver篇) 书接上回,首先我们要考虑的是在Driver层面,有哪些东西需要维持状态,只有在需要维持状态的情况下才需要容错,总的来说, ...
- Kinetic使用注意点--image
new Image(config) 参数: config:包含所有配置项的对象. { image: "图片对象", crop: "图片裁剪对象", fill: ...
- 十四、mysql 分区之 HASH && KEY
.hash分区 PS::个人觉得HASH分区很好很强大,简单确分布极其均匀 创建实例: CREATE TABLE HASH_EMP ( tid int, tname ) ) PARTITION ; 将 ...
- 四、mysql内置函数
.字符串函数 concat('a','b'); 字符串拼接函数 ,,"我是A我是B"): 从指定位置开始替换指定长度的指定数据(起步为1) lower() 转小写 upper() ...
- 一、mysql使用入门
mysql -h localhost -u root -p123456 登录mysql服务器 show databases 列出所拥有的数据库 use www 选择一个www的数据库 show tab ...
- mysql group by优化
mysql> explain select actor.first_name,actor.last_name,count(*) from sakila.film_actor inner join ...