HDU 4028 The time of a day By Assassin dp+离散化!

The time of a day

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

题目大意,一个表有一个1-n的指针,每个指针的周期就是i本身
,钟表的一天,也就是一个周期为从0开始到所有指针回到0状态的时间,给定m找出比m值大的周期的组合的数量。

直接状压dp是不行的,因为有大概n=40,成了1e12了,绝对爆炸,思路肯定是dp,且找出每次序列的lcm最小公倍数,统计大于m的数就好了,那么怎么做!离散化!

怎么离散化?我们看实际上最小公倍数赎回重合的比如{2,3}和{1,6}就是重合的,那么我们用一位map数组,**每次map [i]【j】
代表在i位置,最小公约数的情况有多少种!然后我们可以利用map的迭代器进行离散化!**

下面直接上代码了,一些注释已经加到代码中了,希望可以帮助大家

#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
map<long long,long long>dp[50];  //map用迭代器是可以离散化的dp[i][j]代表当前第i位置公倍数为j的情况数 

long long gcd(long long a,long long b){
    return a%b==0?b:gcd(b,a%b);
} 

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

void make_dp(){
    int i,j;
    dp[1][1]=1;  //初始化,而且只有一个也只有这一种情况了 
    for(i=2;i<=40;i++) {
        dp[i]=dp[i-1];  //这个至关重要!这样的话就可以把之前的所有的状态都转移了过来,类似离散的状压了! 
        //上面一部相当于加上了不选择i位置的情况数! 
        dp[i][i]+=1;   //每次代表将只选择i位置的情况加上,从000到001是需要加1的 
        for(map<long long,long long> ::iterator it=dp[i-1].begin();it!=dp[i-1].end();it++){
            long long tmp=lcm(it->first,i); //当前选择i位置更新 
            dp[i][tmp]+=it->second;
        }
    } 
}

int main(){
    int t,i,j,k;
    long long n,m;
    make_dp();
    while(scanf("%d",&t)!=EOF){
        for(k=1;k<=t;k++){
            scanf("%lld%lld",&n,&m);
            printf("Case #%d: ",k);
            long long ans=0;
            for(map<long long,long long> ::iterator it=dp[n].begin();it!=dp[n].end();it++){
                if(it->first>=m){
                    ans+=it->second;
                }
            }
            cout<<ans<<endl;
        }
    }
    return 0;
} 

还有就是之前对map了解太粗浅了!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值