Permutation

Problem Description
There is an arrangement of N numbers and a permutation relation that alter one arrangement into another.
For example, when N equals to 6 the arrangement is 123456 at first. The replacement relation is 312546 (indicate 1->2, 2->3, 3->1, 4->5, 5->4, 6->6, the relation is also an arrangement distinctly).
After the first permutation, the arrangement will be 312546. And then it will be 231456.
In this permutation relation (312546), the arrangement will be altered into the order 312546, 231456, 123546, 312456, 231546 and it will always go back to the beginning, so the length of the loop section of this permutation relation equals to 6.
Your task is to calculate how many kinds of the length of this loop section in any permutation relations.
 
Input
Input contains multiple test cases. In each test cases the input only contains one integer indicates N. For all test cases, N<=1000.
 
Output
For each test case, output only one integer indicates the amount the length of the loop section in any permutation relations.
 
Sample Input
1
2
3
10
Sample Output
1
2
3
16
 
题意:
    有N个元素的一个集合经过K次置换能变回原来的集合,求k的方案数。
    例如:n=10,
    有如下方案:1,2,3,4,5,6,7,8,9,2*5,4*3,2*7,2*9,3*5,4*5,3*7;  一共16种
题解:
   不难发现,就是求每个置换集合的lcm.
   我们可以用dp来求解:
   dp[n][i]:表示前i种质数表示的数和为n的ways
   dp[n][i]=dp[n][i-1]+dp[n-j*prim[i]]][i]        j∈(0,1,2......n/prim[i]); 
 
 
///
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
typedef __int64 ll;
using namespace std;
#define inf 10000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
int gcd(int a,int b)
{
if(b==)return a;
return gcd(b,a%b);
}
//***************************************************************
ll dp[][];
ll prim[];
ll hashs[];
ll cnt;
ll dfs(ll n,ll p)
{
if(dp[n][p])return dp[n][p];
if(n<prim[p])
{
dp[n][p]=;
return ;
}
dp[n][p]=dfs(n,p+);
ll tmp=prim[p];
while(tmp<=n)
{
dp[n][p]+=dfs(n-tmp,p+);
tmp*=prim[p];
}
return dp[n][p]; }
int main()
{ll n;
cnt=;
for(ll i=;i<=;i++)
{
if(hashs[i]==)
{
prim[cnt++]=i;
for(ll j=i+i;j<=;j+=i)hashs[j]=;
} }
memset(dp,,sizeof(dp));
while(scanf("%I64d",&n)!=EOF){
printf("%I64d\n",dfs(n,));
}
return ;
}

代码狗

HDU 4345 Permutation dp的更多相关文章

  1. hdu 4345 Permutation 记忆化搜索

    思路:实际上求的是和小于等于n的质数的种类数!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorit ...

  2. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  3. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  4. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...

  5. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. hdu 4283 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  8. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  9. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

随机推荐

  1. linux 定时 svn 代码更新,配置文件不修改

    普通参数: 普通参数为正常的传参数:  例子:  f1("111") 指定参数: 指定参数为指定哪个参数给函数方法里面某个形式参数专用,优点:不受传参数的位置约束.   例子:  ...

  2. MYSQL远程登录权限设置(转)

    Mysql默认关闭远程登录权限,如下操作允许用户在任意地点登录: 1. 进入mysql,GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY ...

  3. 保存vim的ide环境

    开发周期不是一两天, 要把当前的窗口布局, 命令历史/寄存器历史等保存下来,以便下次编写时快速恢复. 需要保存两个方面的信息: session: 保存窗口的view试图窗口布局, 和全局设置   :m ...

  4. 如何在WordPress中使用七牛云存储

    序:七牛云存储可以方便的将网站的图片等数据镜像到七牛云存储的空间,直接从云端将数据返回给用户.这样可以大大节省网站的空间,提升网站的访问速度. 真正显示一键实现WordPress博客静态文件CDN加速 ...

  5. matlab 之cov 协方差

    COV 1.cov(x) 如果x为向量,返回x的方差 计算方法为: S为方差. 2.cov(X) 如果X为矩阵,把矩阵X的行作为观察值,把列作为变量,返回X的协方差矩阵: diag(cov(X))是每 ...

  6. Request请求总结

    Request.ServerVariables["Url"] 返回服务器地址 Request.ServerVariables["Path_Info"] 客户端提 ...

  7. iOS设备屏幕像素总览

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/4011728.html ,转载请注明出处.   本文永久地址为http://www.cnblogs.com/Ch ...

  8. MyISAM 和 InnoDB 讲解

    MyISAM 和 InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处 ...

  9. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  10. DBhelper封装jdbc并解决特殊字符的问题

    1.测试数据库 1.1 数据库数据 1.2 数据库结构 1.3 数据库脚本 DROP TABLE IF EXISTS `school`.`student`; CREATE TABLE `school` ...