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]); 
 
 
  1. ///
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <queue>
  11. #include <typeinfo>
  12. #include <map>
  13. typedef __int64 ll;
  14. using namespace std;
  15. #define inf 10000000
  16. inline ll read()
  17. {
  18. ll x=,f=;
  19. char ch=getchar();
  20. while(ch<''||ch>'')
  21. {
  22. if(ch=='-')f=-;
  23. ch=getchar();
  24. }
  25. while(ch>=''&&ch<='')
  26. {
  27. x=x*+ch-'';
  28. ch=getchar();
  29. }
  30. return x*f;
  31. }
  32. int gcd(int a,int b)
  33. {
  34. if(b==)return a;
  35. return gcd(b,a%b);
  36. }
  37. //***************************************************************
  38. ll dp[][];
  39. ll prim[];
  40. ll hashs[];
  41. ll cnt;
  42. ll dfs(ll n,ll p)
  43. {
  44. if(dp[n][p])return dp[n][p];
  45. if(n<prim[p])
  46. {
  47. dp[n][p]=;
  48. return ;
  49. }
  50. dp[n][p]=dfs(n,p+);
  51. ll tmp=prim[p];
  52. while(tmp<=n)
  53. {
  54. dp[n][p]+=dfs(n-tmp,p+);
  55. tmp*=prim[p];
  56. }
  57. return dp[n][p];
  58.  
  59. }
  60. int main()
  61. {ll n;
  62. cnt=;
  63. for(ll i=;i<=;i++)
  64. {
  65. if(hashs[i]==)
  66. {
  67. prim[cnt++]=i;
  68. for(ll j=i+i;j<=;j+=i)hashs[j]=;
  69. }
  70.  
  71. }
  72. memset(dp,,sizeof(dp));
  73. while(scanf("%I64d",&n)!=EOF){
  74. printf("%I64d\n",dfs(n,));
  75. }
  76. return ;
  77. }

代码狗

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. Iterator<转>

    Iterator就是迭代器的意思. Iterator是一个接口,利用迭代器主要是获取元素,很少删除元素.有三个方法:    1)hasNext():判断是否有更多的元素,如果有返回true.    2 ...

  2. spring mvc实现修改+删除

    1.在userController中添加修改的方法 a.首先点击修改,我们一般是到修改界面,并且上面有值,并且有提交按钮 b.修改后,提交到查看的页面 //进入修改界面 @RequestMapping ...

  3. Visual Studio Online Integrations-Productivity

                                        原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-di ...

  4. 简单的分页存储过程,Json格式日期转换为一般日期

    简单的分页存储过程 CREATE PROC Paged @pageIndex INT, @pageCount INT OUTPUT, @pageSize INT AS DECLARE @count I ...

  5. c# 改变图片的大小(w,h)

    本文介绍获取网络上的图片将其大小尺寸改成自己想要的 /// <summary> /// 图片大小裁剪 /// </summary> /// <param name=&qu ...

  6. Java主流日志工具库

    在项目开发中,为了跟踪代码的运行情况,常常要使用日志来记录信息.在Java世界,有很多的日志工具库来实现日志功能,避免了我们重复造轮子.我们先来逐一了解一下主流日志工具. 1.java.util.lo ...

  7. javascript 对象(DOM)document window history

    Javascript对象 目录: window对象 document对象 history对象 navigator对象 window对象 所有浏览器都支持window对象,它表示浏览器窗口. 所有jav ...

  8. 如何看K线图基础知识

    在日K线图中一般白线.黄线.紫线.绿线依次分别表示:5.10.20.60日移动平均线,但这并不是固定的,会根据设置的不同而不同,比如你也可以在系统里把它们设为5.15.30.60均线. 你看K线图的上 ...

  9. java笔记--查看和修改线程的优先级

    查看和修改线程的优先级 java中每一个线程都有优先级属性,在默认情况下,新建的线程的优先级与创建该线程的线程优先级相同.每当线程调度器选择要运行的线程时,通常选择优先级较高的线程. 注:线程的优先级 ...

  10. 开机提示grub可咋办啊

    导读 GRUB是多启动规范的实现,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统.GRUB可用于选择操作系统分区上的不同内核,也可用于向这些内核传递启动参数. 1 ...