【BZOJ】1025: [SCOI2009]游戏(置换群+dp+特殊的技巧+lcm)
http://www.lydsy.com/JudgeOnline/problem.php?id=1025
首先根据置换群可得
$$排数=lcm\{A_i, A_i表示循环节长度\}, \sum_{i=1}^{k} A_i = n$$
根据lcm的定义,分解质因数拆掉$A_i=p_1^{x_1} \times p_2^{x_2} \times ... \times p_k^{x_k}$后
$$lcm=\prod_{i} p_i^{max\{x_i\}}$$
所以我们只看$max\{x_i\}$即可,即忽略掉$\le max\{x_i\}$的其它因子。所以问题等价于:
$$\sum_{i} p_i^{x_i} \le n$$
的方案数。
然后随便dp即可
设$d(i,j)$表示前$i$个质数和为$j$的方案,有
$$d(i,j)=d(i-1,j)+\sum_{k} d(i-1, j-p_i^k) $$
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005;
int p[N], vis[N], cnt, n;
ll d[2][N];
void init() {
for1(i, 2, n) {
if(!vis[i]) p[++cnt]=i;
for(int j=1; j<=cnt && p[j]*i<=n; ++j) {
vis[p[j]*i]=1;
if(!(i%p[j])) break;
}
}
} int main() {
read(n); init();
int now=1, last=0;
d[last][0]=1;
for1(i, 1, cnt) {
for1(j, 0, n) {
d[now][j]=d[last][j];
for(int k=p[i]; j-k>=0; k*=p[i])
d[now][j]+=d[last][j-k];
}
for1(j, 0, n) d[last][j]=0;
now^=1;
last^=1;
}
ll ans=0;
for1(i, 0, n) ans+=d[last][i];
printf("%lld", ans);
return 0;
}
Description
windy学会了一种游戏。对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应。最开始windy把数字按顺序1,2,3,……,N写一排在纸上。然后再在这一排下面写上它们对应的数字。然后又在新的一排下面写上它们对应的数字。如此反复,直到序列再次变为1,2,3,……,N。 如: 1 2 3 4 5 6 对应的关系为 1->2 2->3 3->1 4->5 5->4 6->6 windy的操作如下 1 2 3 4 5 6 2 3 1 5 4 6 3 1 2 4 5 6 1 2 3 5 4 6 2 3 1 4 5 6 3 1 2 5 4 6 1 2 3 4 5 6 这时,我们就有若干排1到N的排列,上例中有7排。现在windy想知道,对于所有可能的对应关系,有多少种可能的排数。
Input
包含一个整数,N。
Output
包含一个整数,可能的排数。
Sample Input
3
【输入样例二】
10
Sample Output
3
【输出样例二】
16
HINT
【数据规模和约定】
100%的数据,满足 1 <= N <= 1000 。
Source
【BZOJ】1025: [SCOI2009]游戏(置换群+dp+特殊的技巧+lcm)的更多相关文章
- BZOJ 1025: [SCOI2009]游戏 [置换群 DP]
传送门 题意:求$n$个数组成的排列变为升序有多少种不同的步数 步数就是循环长度的$lcm$..... 那么就是求$n$划分成一些数几种不同的$lcm$咯 然后我太弱了这种$DP$都想不出来.... ...
- BZOJ 1025: [SCOI2009]游戏( 背包dp )
显然题目要求长度为n的置换中各个循环长度的lcm有多少种情况. 判断一个数m是否是满足题意的lcm. m = ∏ piai, 当∑piai ≤ n时是满足题意的. 最简单我们令循环长度分别为piai, ...
- [BZOJ 1025] [SCOI2009] 游戏 【DP】
题目链接:BZOJ - 1025 题目分析 显然的是,题目所要求的是所有置换的每个循环节长度最小公倍数的可能的种类数. 一个置换,可以看成是一个有向图,每个点的出度和入度都是1,这样整个图就是由若干个 ...
- [bzoj 1025][SCOI2009]游戏(DP)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1025 分析:首先这个问题等价于A1+A2+……Ak=n,求lcm(A1,A2,……,Ak)的种 ...
- BZOJ 1025 [SCOI2009]游戏
1025: [SCOI2009]游戏 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1533 Solved: 964[Submit][Status][ ...
- bzoj 1025 [SCOI2009]游戏(置换群,DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1025 [题意] 给定n,问1..n在不同的置换下变回原序列需要的不同排数有多少种. [ ...
- BZOJ 1025 [SCOI2009]游戏 (DP+分解质因子)
题意: 若$a_1+a_2+\cdots+a_h=n$(任意h<=n),求$lcm(a_i)$的种类数 思路: 设$lcm(a_i)=x$, 由唯一分解定理,$x=p_1^{m_1}+p_2^{ ...
- bzoj 1025: [SCOI2009]游戏【数学+dp】
很容易发现行数就是lcm环长,也就是要求和为n的若干数lcm的个数 有结论若p1^a1+p2^a2+...+pm^am<=n,则ans=p1^a1p2^a2..*pm^am是n的一个可行答案.( ...
- BZOJ 1025 SCOI2009 游戏 动态规划
标题效果:特定n.行定义一个替代品1~n这种更换周期发生后,T次要(T>0)返回到原来的顺序 找到行的所有可能的数 循环置换分解成若干个,然后行位移数是这些周期的长度的最小公倍数 因此,对于一些 ...
随机推荐
- iOS开发之UITableView的使用
这一篇记录的是iOS开发中UITableView的使用,iOS中的UITableView跟Android中的ListView特别相似,以下用一个Demo来说明: 1.Xcode中新建projectTe ...
- ubuntu 设置计划任务
非常简便: crontab -e 以下是我的执行过程,输入命令后,会让我选择一个编辑器,我选的是2,因为后边写着easiest,最简单的. liuyx@myubuntu:/$ crontab -e n ...
- JavaScript 中 substr 和 substring的区别
http://www.cnblogs.com/meil/archive/2007/01/05/598528.htmlString.substr(N1,N2) 这个就是我们常用的从指定的位置(N1)截取 ...
- JS 对象的属性如果没有就初始化
function fuck (inObj, path, parms) { // 一个长得像对象的字符串 var Things = path.split("."); // 即将返回的 ...
- java开发中国际化
1 静态文本的国际化,就是比如页面中中文显示用户名就是用户名,用于显示就是 username. 其中静态文件命名遵循:基础名_语言简称_国家简称.properties 需要使用的类是 1)import ...
- MySQL主从不一致情形与解决方法
参考:https://blog.csdn.net/hardworking0323/article/details/81046408 https://blog.csdn.net/lijingkuan/a ...
- 开发avr单片机网络资源
1.avr用的c语言标准库 http://www.nongnu.org/avr-libc/ 2.avr的下载上传器 http://www.nongnu.org/avrdude/ 3.编程环境platf ...
- C++加载dll失败或显示乱码
右键项目-属性-字符集-使用多字节字符集
- javascript递归、循环、迭代、遍历和枚举概念
javascript递归.循环.迭代.遍历和枚举概念 〓递归(recursion)在数学与计算机科学中,是指在函数的定义中使用函数自身的方法.递归一词还较常用于描述以自相似方法重复事物的过程.例如,当 ...
- [svc]entrypoint.sh shell脚本解析
最近搞influxdb绘图,看到其dockerfile的entry.sh,无奈看的不是很懂. 于是查了下.. docker run 通过传参实现配置文件覆盖 实现启动镜像时候可指定配置文件 如果不指定 ...