hdu4028(离散化dp)

作者: sugar_coated | 来源:发表于2016-10-13 17:22 被阅读32次

Problem Description

There are no days and nights on byte island, so the residents here can hardly determine the length of a single day. Fortunately, they have invented a clock with several pointers. They have N pointers which can move round the clock. Every pointer ticks once per second, and the i-th pointer move to the starting position after i times of ticks. The wise of the byte island decide to define a day as the time interval between the initial time and the first time when all the pointers moves to the position exactly the same as the initial time.The wise of the island decide to choose some of the N pointers to make the length of the day greater or equal to M. They want to know how many different ways there are to make it possible.

Input

There are a lot of test cases. The first line of input contains exactly one integer, indicating the number of test cases. For each test cases, there are only one line contains two integers N and M, indicating the number of pointers and the lower bound for seconds of a day M. (1 <= N <= 40, 1 <= M <= 263
-1)

Output

For each test case, output a single integer denoting the number of ways.

如:


Paste_Image.png

题目大意:给你1~n这n个数,从这n个数中选取x个数(1<=x<=n),使得这些数的最小公倍数(lcm)大于等于m,问你有多少种选法。

这是一道离散化dp的题。以前没有接触过,看了题解后,恍然大悟,又学到了新知识,嘻嘻。不多说了,看代码:

#include <iostream>
#include <vector>
#include <map>
#include <iterator>
using namespace std;

typedef long long ll;
typedef map<ll, ll> mp;//first表示最小公倍数,second表示有多少种情况。
const int MAX_N = 45;

vector<mp> dp(MAX_N);//dp[i][j] = x;表示n为i时,可以有it->second(即x)种情况组成it->first(即j)

ll gcd(ll a, ll b) {//求最大公约数
    return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b) {//求最小公倍数
    return a * b / gcd(a, b);
}

void init() {
    dp[1][1] = 1;
    for (int i = 2; i < MAX_N; ++i) {//表示有i个指针的时候
        dp[i] = dp[i - 1];//不取第i个的所有情况
        ++dp[i][i];//只取第i个的情况
        for (mp::iterator it = dp[i - 1].begin(); it != dp[i - 1].end(); ++it)
            dp[i][lcm(i, it->first)] += it->second;//在前i - 1的基础上加上第i个数
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    init();
    int t;
    cin >> t;
    for (int ks = 1; ks <= t; ++ks) {
        ll n, m;
        cin >> n >> m;
        ll ans = 0;
        for (mp::iterator it = dp[n].begin(); it != dp[n].end(); ++it) {//计算满足条件的个数
            if (it->first >= m) ans += it->second;
        }
        cout << "Case #" << ks << ": " << ans << endl;
    }
    return 0;
}

相关文章

  • hdu4028(离散化dp)

    Problem Description There are no days and nights on byte ...

  • 使用spark ml做离散化(分位数离散化)

    单列离散化: 多列同时离散化: 不同离散化方式:http://www.javashuo.com/article/p...

  • 标准化和离散化总结

    离散化总结 等距离散化是根据连续型变量的取值,等频离散化根据连续型变量的总个数。

  • 离散化

    离散化指把连续型数据切分为若干“段”,也称bin,是数据分析中常用的手段。切分的原则有等距,等频,优化,或根据数据...

  • 离散化

    电影 原题链接[https://www.acwing.com/problem/content/105/] 具体离散...

  • 滑雪dp

    经典的dp题:滑雪-dp记忆化深搜 DP 记忆化深搜(1) 如果只有1个点,结果就是1(2) 如果有两个点,从1-...

  • 数据预处理_数据离散化

    一、数据离散化 1、所谓离散化,就是把无限空间中的有限个体映射到一个有限的空间中。 2、数据离散化大多针对连续数据...

  • 特征离散化

    详情可看https://wenku.baidu.com/view/9e45b337011ca300a6c390d2...

  • 连续属性离散化实例

    连续属性离散化,就是将数值型变量转化为离散型变量类似于指标转维度的那种感觉 前面说过,连续属性离散化有几种方式: ...

  • 2019-04-08

    一、离散化 简单离散 通过定义一些划分规则,将原来连续的数据划分成不同的类别,从而将数据离散化。 分桶后平滑 先根...

网友评论

    本文标题:hdu4028(离散化dp)

    本文链接:https://www.haomeiwen.com/subject/lblwyttx.html