hdu4028 The time of a day[map优化dp]

The time of a day

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1297    Accepted Submission(s): 594

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.

Sample Input

3
5 5
10 1
10 128

Sample Output

Case #1: 22
Case #2: 1023
Case #3: 586

Source

The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  4027 4022 4023 4026 4021

#include<map>
#include<cstdio>
using namespace std;
typedef long long ll;
map<ll,ll>f[45];ll m,ans;int n,cas,Cas;
map<ll,ll>::iterator it,ii;
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int main(){
    f[1][1]=1;
    for(int i=1;i<=40;i++){
        f[i]=f[i-1];
        f[i][i]++;
        for(it=f[i-1].begin();it!=f[i-1].end();it++){
            f[i][lcm(it->first,i)]+=it->second;
        }
    }
    for(scanf("%d",&Cas),cas=1;cas<=Cas;cas++){
        scanf("%d%I64d",&n,&m);printf("Case #%d: ",cas);
        ans=0;
        for(it=f[n].begin();it!=f[n].end();it++){
            if(it->first>=m) ans+=it->second;
        }
        printf("%I64d\n",ans);
    }
    return 0;
} 
时间: 2024-09-29 15:47:46

hdu4028 The time of a day[map优化dp]的相关文章

【java】itoo项目实战之大数据查询之使用 new map 优化hibernate之级联查询

在我的上一篇博客<[java]itoo项目实战之hibernate 懒加载优化性能>中,我曾提到过学生数据有2万条,查询数据十分的慢,这是让人很受不了的事情,看着页面进度条一直转着圈圈,那种着急的感觉真的没法形容.最开始考虑着使用lazy 来优化,因为前台框架的原因,lazy 优化并没有起到什么左右,后来就想着有select new map 优化.我先来画画关于查询学生的级联树 这个树的意思就是查询学生的时候它的深度是4级. 在没有优化之前,使用的是hibernate的hql 语句:From

使用Map优化双层For循环

笔者在<for循环实战性能优化>中提出了五种提升for循环性能的优化策略,这次我们在其中嵌套循环优化小循环驱动大循环的基础上,借助Map高效的查询性能来优化双层for循环. 如果小循环和大循环的集合元素数量分别为M和N,则双层For循环的循环次数是M*N,随着M和N的增长,对性能的影响越来越大.因此,本文考虑进一步优化.利用下面的代码来模拟测试两种情况的性能: import java.util.ArrayList; import java.util.List; import java.util

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as

hdu3507之斜率优化DP入门

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 4780    Accepted Submission(s): 1437 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antiqu

BZOJ 1096 [ZJOI2007]仓库建设 斜率优化dp

1096: [ZJOI2007]仓库建设 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1096 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚. 由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天,L公司的总裁L先生接到气象部门的电话,被告知三天之后将有一场

HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程很容易想出来,dp[i][j] 表示前 j 个数分成 i 组.但是复杂度是三次方的,肯定会超时,就要对其进行优化. 有两种方式,一种是斜率对其进行优化,是一个很简单的斜率优化 dp[i][j] = min{dp[i-1][k] - w[k] + sum[k]*sum[k] - sum[k]*sum[

hdu3480之二维斜率优化DP

Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Total Submission(s): 2664    Accepted Submission(s): 1050 Problem Description Little D is really interested in the theorem of sets recently. There's a pro

优化DP的奇淫技巧

DP是搞OI不可不学的算法.一些丧心病狂的出题人不满足于裸的DP,一定要加上优化才能A掉. 故下面记录一些优化DP的奇淫技巧. OJ 1326 裸的状态方程很好推. f[i]=max(f[j]+sum[i]-sum[j]-100*I) (1<=j<i&&f[j]>=100*i) 然后把无关于j的提出来. f[i]=max(f[j]-sum[j])+sum[i]-100*i; 好的,现在只需要把在O(1)的时间内求出max(f[j]-sum[j])就是坠吼得. 考虑两个决策

【转】斜率优化DP和四边形不等式优化DP整理

当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重循环跑状态 i,一重循环跑 i 的所有子状态)这样的时间复杂度是O(N^2)而 斜率优化或者四边形不等式优化后的DP 可以将时间复杂度缩减到O(N) O(N^2)可以优化到O(N) ,O(N^3)可以优化到O(N^2),依次类推 斜率优化DP和四边形不等式优化DP主要的原理就是利用斜率或者四边形不等式等数学方法 在所有要判断的子状态中迅速做出判断,所以这里的优化其实是省去了枚举