BZOJ 1025 游戏(分组背包)

题目所谓的序列长度实际上就是各循环节的lcm+1.

所以题目等价于求出 一串数之和等于n,这串数的lcm种数。

由唯一分解定理可以联想到只要把每个素数的幂次放在一个分组里,然后对整体做一遍分组背包就行了。

 

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-3
# define MOD 1000000007
# define INF (LL)1<<60
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int res=0, flag=0;
    char ch;
    if((ch=getchar())=='-') flag=1;
    else if(ch>='0'&&ch<='9') res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')  res=res*10+(ch-'0');
    return flag?-res:res;
}
void Out(int a) {
    if(a<0) {putchar('-'); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+'0');
}
const int N=1005;
//Code begin...

int pri[N];
LL dp[N][N];

void init(int n)
{
    mem(pri,0);
    FOR(i,2,n) {
        if (!pri[i]) pri[++pri[0]]=i;
        for (int j=1; j<=pri[0]&&pri[j]<=n/i; ++j) {
            pri[pri[j]*i]=1;
            if (i%pri[j]==0) break;
        }
    }
}
int main ()
{
    int n;
    scanf("%d",&n);
    init(n);
    FOR(i,0,n) dp[0][i]=1;
    FOR(i,1,pri[0]) FOR(j,0,n) {
        dp[i][j]=dp[i-1][j];
        for (int k=pri[i]; k<=j; k*=pri[i]) dp[i][j]+=dp[i-1][j-k];
    }
    printf("%lld\n",dp[pri[0]][n]);
    return 0;
}
View Code

 

posted @ 2017-03-01 17:09  free-loop  阅读(157)  评论(0编辑  收藏  举报