【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

 

 
posted @ 2014-11-17 21:02  iwtwiioi  阅读(744)  评论(0编辑  收藏  举报