码迷,mamicode.com
首页 > 其他好文 > 详细

DP(优化) UVALive 6073 Math Magic

时间:2015-10-28 22:26:06      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

 

 

/************************************************
* Author        :Running_Time
* Created Time  :2015/10/28 星期三 20:20:09
* File Name     :H.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int M = 1e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int dp[2][N][N];
int lcm[N][N];
int vec[N];

int GCD(int a, int b)   {
    return b ? GCD (b, a % b) : a;
}

void init(void) {
    for (int i=1; i<=1000; ++i) {
        for (int j=1; j<=1000; ++j) {
            lcm[i][j] = i * j / GCD (i, j);
        }
    }
}

inline void add(int &x, int y)   {
    x += y;
    if (x > MOD)    x -= MOD;
}

int main(void)    {
    init ();
    int n, m, k;
    while (scanf ("%d%d%d", &n, &m, &k) == 3)   {
        int t = 0;
        for (int i=1; i<=m; ++i)    {
            if (m % i == 0) vec[t++] = i;
        }
        int now = 0;
        for (int i=0; i<=n; ++i)    {
            for (int j=0; j<t; ++j) {
                dp[now][i][vec[j]] = 0;
            }
        }
        dp[now][0][1] = 1;
        for (int l=1; l<=k; ++l)    {
            now ^= 1;
            for (int i=0; i<=n; ++i)    {
                for (int j=0; j<t; ++j) {
                    dp[now][i][vec[j]] = 0;
                }
            }
            for (int i=l-1; i<=n; ++i)  {
                for (int j=0; j<t; ++j) {
                    if (dp[now^1][i][vec[j]] == 0)   continue;
                    for (int p=0; p<t; ++p) {
                        int x = i + vec[p];
                        int y = lcm[vec[j]][vec[p]];
                        if (x > n || m % y != 0)    continue;
                        dp[now][x][y] = (dp[now][x][y] + dp[now^1][i][vec[j]]) % MOD;
                    }
                }
            }
        }
        printf ("%d\n", dp[now][n][m]);
    }

   //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";

    return 0;
}

  

DP(优化) UVALive 6073 Math Magic

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4918649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!