hdu4028(离散化dp)

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,692评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,482评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,995评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,223评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,245评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,208评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,091评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,929评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,346评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,570评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,739评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,437评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,037评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,677评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,833评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,760评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,647评论 2 354

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,476评论 0 23
  • Why Must-Have 写完iOS篇后认真思考了一下自己使用App的情况,发现确实有的App的使用率不高,决定...
    WindsorDuke阅读 2,271评论 0 41
  • 你曾说,那年杏花微雨,是一生最美的意外。 后来,终是烟消云散,徒留我独自羁绊。 你曾说...
    叫我小姐姐啦阅读 311评论 0 4
  • 你给的亲密 看似亲密 但总不逾矩。 微笑起来嘴角牵起的弧度刚刚好 挑不出毛病 却比不上露出满口的牙齿 把眼睛挤成一...
    999zxx阅读 273评论 1 3