hdu4028 离散DP

以后一定要每天都写报告

题意:40个数,选出几个,使其lcm大于等于m,计数DP, map

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#define inf (1 << 30)
const double eps = 1e-12;
const double pi = acos(-1.0);
using namespace std;

int T;
__int64 n, m;
map<__int64, __int64> dp[41];
map<__int64, __int64> :: iterator iter;

__int64 gcd(__int64 a, __int64 b)
{
    return a % b ? gcd(b, a % b) : b;
}

__int64 lcm(__int64 a, __int64 b)
{
    return a * b / gcd(a, b);
}

int solve()
{
    int i;
    __int64 first, second, t1;
    //dp[1].insert(make_pair(1, 1));
    dp[1][1] = 1;
    for (i = 2; i <= 40; i++)
    {
        dp[i] = dp[i - 1];
        dp[i][i]++;
        for (iter = dp[i - 1].begin(); iter != dp[i - 1].end(); iter++)
        {
            first = iter -> first, second = iter -> second;
            t1 = lcm(first, i);
            dp[i][t1] += second;
        }
    }
    return 1;
}

int main()
{
    int i, j, h;
    __int64 ans, first, second;
    solve();
    scanf("%d", &T);
    for (h = 1; h <= T; h++)
    {
        scanf("%I64d%I64d", &n, &m);
        printf("Case #%d: ", h);
        ans = 0;
        for (iter = dp[n].begin(); iter != dp[n].end(); iter++)
        {
            first = iter -> first, second = iter -> second;
            if (first >= m) ans += second;
        }
        printf("%I64d\n", ans);
    }
    system("pause");
    return 0;
}

你可能感兴趣的:(hdu4028 离散DP)