1025: [SCOI2009]遊戲
Time Limit: 1 Sec Memory Limit:162 MBSubmit: 1594 Solved: 1003
[Submit][Status][Discuss]
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 。
先YY一下,本題題意爲:將N拆分成若干個數並計算他們的LCM,問不同的LCM個數。
設N = A1 + A2 + ... + A[t] 再將每個數A分解成Pi ^ Bi的乘積,則LCM = Pi ^ max(Bi) 的乘積(媽蛋CSDN有公式編輯器麼)那麼對於每個Pi枚舉Bi即可。
令F[i][j]表示在前i個質數內選擇若干個且和爲j的方案數,則:F[i][j] = F[i - 1][j] + F[i - 1][j - P[i] ^ k].
代碼如下:
/**************************************************************
Problem: 1025
User: duyixian
Language: C++
Result: Accepted
Time:20 ms
Memory:20844 kb
****************************************************************/
/*
* @Author: 逸閒
* @Date: 2015-10-01 11:49:05
* @Last Modified by: 逸閒
* @Last Modified time: 2015-10-01 16:03:23
*/
#include "cstdio"
#include "cstdlib"
#include "iostream"
#include "algorithm"
#include "cstring"
#include "queue"
using namespace std;
#define INF 0x3F3F3F3F
#define MAX_SIZE 5000
#define Eps
#define Mod
inline int Get_Int()
{
int Num = 0;
char ch;
do
ch = getchar();
while(ch < '0' || ch > '9');
do
{
Num = Num * 10 + ch - '0';
ch = getchar();
}
while(ch >= '0' && ch <= '9');
return Num;
}
int N, Total;
int Flag[MAX_SIZE], P[MAX_SIZE];
long long Ans;
long long DP[500][MAX_SIZE];
int main()
{
cin >> N;
for(int i = 2; i <= N; ++i)
if(!Flag[i])
{
P[++Total] = i;
for(int j = i + i; j <= N; j += i)
Flag[j] = true;
}
DP[0][0] = 1;
for(int i = 1; i <= Total; ++i)
for(int j = 0; j <= N; ++j)
{
DP[i][j] = DP[i - 1][j];
for(int k = P[i]; k <= j; k *= P[i])
DP[i][j] += DP[i - 1][j - k];
}
for(int i = 0; i <= N; ++i)
Ans += DP[Total][i];
cout << Ans << endl;
return 0;
}