ZOJ 3662 Math Magic(12年长春现场赛 H)

思路来自 http://blog.csdn.net/acm_cxlove/article/details/7854526   by---cxlove 


题目:给出K个数,使得这K个数的和为N,LCM为M,问有多少种

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4885 

LCM为M,那么中间状态的LCM肯定为M的约数,而且加入的数也肯定是M的约数

dp[i][j][k]表示取了i个数,和为j,LCM为状态k的时候的种数

ZOJ卡得很紧,还需要预处理LCM


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
#include <complex>
#include <set>
#include <deque>
#define DEBUG(x) cerr<<"line:"<<__LINE__<<", "<<#x" == "<<(x)<<endl;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define FOR(it,s) for(__typeof(s.begin()) it=s.begin();it!=s.end();it++)
#define ALL(a) (a).begin(),(a).end()
#define RALL(x) (a).rbegin(),(a).rend()
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define DRI(x) int (x);scanf("%d",&(x))
#define DRII(x,y) int (x),(y);scanf("%d%d",&(x),&(y))
#define DRIII(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z))
#define MS0(a) memset((a),0,sizeof((a)))
#define MS1(a) memset((a),-1,sizeof((a)))
#define MS(a,b) memset((a),(b),sizeof((a)))
#define PB push_back
#define SZ(a) (int)(a).size()

using namespace std;

typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;

#define INF 1000000000
const double eps = 1e-10;
int dcmp(double x){
  if(fabs(x) < eps) return 0;
  else return x < 0 ? -1 : 1;
}
// ------------------
// author : onehrxn
// ------------------
const int maxn = 1000+10;
const int maxm = 1000+10;
int dp[3][maxn][maxm];
vi num;
inline int lcm(int a, int b){
  return a / __gcd(a,b) * b;
}
int LCM[maxn][maxn];
const int mod = 1e9 + 7;
int main(void)
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef LOCAL
    // freopen("input", "r", stdin);
    // freopen("output", "w", stdout);
#endif
  for(int i = 1; i <= 1000; i++){
    for(int j =1; j <= 1000; j++)
      LCM[i][j] = lcm(i,j);
  }
  int N, M, K;
  while(cin >> N >> M >> K){
    num.clear();
    for(int i = 1; i <= M; i++) if(M%i == 0) num.PB(i);
    int size = num.size();

    int now = 0;
    for(int i = 0; i <= N; i++){
      for(int j = 0; j < size; j++)
        dp[now][i][num[j]] = 0;
    }
    dp[now][0][1] = 1;
    for(int t = 1; t <= K; t++){
      now ^= 1;
      for(int i = 0; i <= N; i++){
        for(int j = 0; j < size; j++)
          dp[now][i][num[j]] = 0;
      }

      for(int i = t-1; i <= N; i++){

        for(int j = 0; j < size; j++){
          if(dp[now^1][i][num[j]] == 0) continue;

          for(int p = 0; p < size; p++){
            int tmpx = num[p] + i;
            int tmpy = LCM[num[j]][num[p]];
            dp[now][tmpx][tmpy] += dp[now^1][i][num[j]];
            dp[now][tmpx][tmpy] %= mod;
          }
        }
      }
    }
    printf("%d\n", dp[now][N][M]);
  }
#ifdef LOCAL
        cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值