数论+DP HDOJ 4345 Permutation
题意:一个置换群,经过最少k次置换后还原。问给一个N个元素,在所有的置换群里,有多少个不同的k。
分析:这道题可以转化成:N = Σ ai ,求LCM ( ai )有多少个不同的值。比如N=10时,k可为:1,2,3,2*2,5,2*3,7,2*2*2,3*3,2*5,2*2*3,2*7,3*5,2*2*5,3*7,2*3*5,共16个,这里用到了唯一分解定理:每个大于1的自然数均可写为质数的积,而且这些素因子按大小排列之后,写法仅有一种方式。例如: 。那么先预处理出1000内的素数,dp[i][j]表示用到了前i个素数,组成和为j,的个数,一个素数可能用到多次。因为1对结果不影响,所以结果是Σ dp[m][i] (m最大的素数<=n,i<=n)
收获:1. 置换群和唯一分解定理 2. DP和数论结合
代码:
/************************************************
* Author :Running_Time
* Created Time :2015-8-27 18:11:40
* File Name :F.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int prime[N/2];
bool is_prime[N];
ll dp[200][N]; int seive(void) {
int p = 0;
memset (is_prime, true, sizeof (is_prime));
for (int i=2; i<=1000; ++i) {
if (is_prime[i]) prime[++p] = i;
for (int j=1; j<=p && prime[j]*i<=1000; ++j) {
is_prime[i*prime[j]] = false;
if (i % prime[j] == 0) break;
}
}
return p;
} int main(void) {
int p = seive ();
int n, m = 0;
while (scanf ("%d", &n) == 1) {
memset (dp, 0, sizeof (dp));
dp[0][0] = 1;
for (int i=1; i<=p; ++i) {
for (int j=0; j<=n; ++j) dp[i][j] = dp[i-1][j];
int res = prime[i];
if (res <= n) m = i;
else break;
while (res <= n) {
for (int j=0; j+res<=n; ++j) {
if (dp[i-1][j]) dp[i][j+res] += dp[i-1][j];
}
res *= prime[i];
}
}
ll ans = 0;
for (int i=1; i<=n; ++i) ans += dp[m][i];
printf ("%I64d\n", ans + 1);
} return 0;
}
数论+DP HDOJ 4345 Permutation的更多相关文章
- 【bzoj1408】[Noi2002]Robot 数论+dp
题目描述 输入 输出 样例输入 3 2 1 3 2 5 1 样例输出 8 6 75 题解 语文题+数论+dp 花了大段讲述什么叫mu,什么叫phi,只是新定义的mu将2看作有平方因子,新定义的phi( ...
- 洛谷$P5366\ [SNOI2017]$遗失的答案 数论+$dp$
正解:数论$dp$ 解题报告: 传送门$QwQ$ 考虑先质因数分解.所以$G$就相当于所有系数取$min$,$L$就相当于所有系数取$max$ 这时候考虑,因为数据范围是$1e8$,$1e8$内最多有 ...
- HDU 4345 Permutation dp
Permutation Problem Description There is an arrangement of N numbers and a permutation relation that ...
- 【HDOJ】4345 Permutation
即求P1^n1+P2^n2 + ... + Pk^nk <= n,其中Pk为素数的所有可能组合.思路是DP.1~1000的素数就不到200个.dp[i][j]表示上式和不超过且当前最小素数为P[ ...
- 找规律/数位DP HDOJ 4722 Good Numbers
题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...
- 递推DP HDOJ 5328 Problem Killer
题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...
- 数学+dp HDOJ 5317 RGCDQ
题目传送门 /* 题意:给一个区间,问任意两个数的素数因子的GCD最大 数学+dp:预处理出f[i],发现f[i] <= 7,那么用dp[i][j] 记录前i个f[]个数为j的数有几个, dp[ ...
- 递推DP HDOJ 5092 Seam Carving
题目传送门 /* 题意:从上到下,找最短路径,并输出路径 DP:类似数塔问题,上一行的三个方向更新dp,路径输出是关键 */ #include <cstdio> #include < ...
- 递推DP HDOJ 5389 Zero Escape
题目传送门 /* 题意:把N个数分成两组,一组加起来是A,一组加起来是B,1<=A,B<=9,也可以全分到同一组.其中加是按照他给的规则加,就是一位一位加,超过一位数了再拆分成一位一位加. ...
随机推荐
- HDU3459:Rubik 2×2×2(IDA)
Problem Description Sonny is probably the only computer science Ph.D. student who cannot solve a Rub ...
- 关键路径(AOE)---《数据结构》严蔚敏
// exam1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- C#调用国家气象局天气预报接口
原文:C#调用国家气象局天气预报接口 一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天 ...
- C#数据库连接池 MySql SqlServer
查阅了一天的资料来学习MySql数据库连接池,终于在一篇博文上找到了,自己也整理了一下,希望对大家有用处 1. 建立连接池 using MySql.Data.MySqlClient; using Sy ...
- android各种菜单使用介绍
Android菜单的有这几种: 1,OptionMenue:选项菜单 2,contextMenu:上下文菜单 3,SubMenu子菜单 其中,OptionMenue与contextMenu的区别(Op ...
- C项目实践--图书管理系统(2)
前面在<<C项目实践-图书管理系统(1)>>中把系统中的三大功能模块中可能涉及到的常量,结构体及相关函数进行了声明定义,下来就来实现它们. 执行系统首先从登录到系统开始,所以首 ...
- HTTP要点概述:四,HTTP方法
使用HTTP协议的时候,客户端可以通过HTTP方法告知服务器自己请求的意图. 看了这篇文章以后,谁再说HTTP方法只有GET和POST,你的眼睛是用来吃饭的嘛! 一,GET:获取资源 GET用来请求访 ...
- 设置Table边框的CSS
<!DOCTYPE html> <html> <head> <style> table, td, th { border: 1px solid blac ...
- JPEG库在Linux系统下的编译和移植【转】
本文转载自: 这篇文章介绍了jpeg库在Linux系统下的编译和移植,经过了亲自的验证,编译首先需要准备以下资源:jpegsrc.v6b.tar.gz(jpeg库),libtool-1.5.26.ta ...
- get the page name from url
https://stackoverflow.com/questions/1874532/better-way-to-get-page-name The way I interpret the ques ...