HDU 4028 The time of a day

来源:互联网 发布:mac 7z解压软件下载 编辑:程序博客网 时间:2024/04/28 03:52

看完题目后觉得要用dp,但是数据量太大,搞不定。看了解题报告后才知道可以用map来离散化。代码如下:

#include <iostream>#include <cstdio>#include <map>using namespace std;const int MAXN = 42;__int64 gcd(__int64 x, __int64 y){    if (y == 0) return x;    return gcd(y, x % y);}__int64 lcm(__int64 x, __int64 y){    return x / gcd(x, y) * y;}int main(){    int t;    __int64 n, m, ans;    map<__int64, __int64> dp[MAXN];    map<__int64, __int64>::iterator mit;        dp[1][1] = 1;    for (int i = 2; i <= 40; ++i)    {        dp[i]= dp[i-1];        dp[i][i]++;        for (mit = dp[i-1].begin(); mit != dp[i-1].end(); ++mit)        {            dp[i][lcm(i, mit->first)] += mit->second;        }    }        scanf("%d", &t);    for (int cas = 1; cas <= t; ++cas)    {        scanf("%I64d %I64d", &n, &m);        ans = 0;        for (mit = dp[n].begin(); mit != dp[n].end(); ++mit)        {            if (mit->first >= m)                ans += mit->second;        }        printf("Case #%d: %I64d\n", cas, ans);    }    return 0;}