HDU - 4028 The time of a day (离散dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4028点击打开链接

The time of a day

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


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 <= 2 63-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
 

Recommend
lcy

dp【i】【j】表示成前i个数能够组成lcm等于j的值有多少个 其中j需要用map离散化

注意这里判断map里之前有没有过当前这个lcm的是后 会自动将map【now】=0; 所以注意不要把这些算在枚举上一行所有lcm里面 用一个map维护即可 因为从小到大 map里的个数是不断增长的

预处理之后用总数-dp值 即得到大于等于的值 因为0 0不算一种所以记得减一。。


#include<bits/stdc++.h>
using namespace std;
long long int dp[44][66666];
map<long long int ,long long int > mp;
map<long long int ,long long int > ::iterator it;
int ccnt=1;
long long int lcm(long long int a,long long int b)
{
    return a*b/__gcd(a,b);
}
int main()
{
    for(long long int i=1;i<=40;i++)
    {
        vector<pair<long long int ,long long int> > s;
        for(it=mp.begin();it!=mp.end();it++)
        {
            long long int p=lcm(it->first,i);
            if(mp[p]==0)
            {
                s.push_back(make_pair(p,it->first));
            }
            else
            {
                dp[i][mp[p]]+=dp[i-1][mp[it->first]];
            }
            dp[i][mp[it->first]]+=dp[i-1][mp[it->first]];
        }
        long long int len=s.size();
        for(long long int j=0;j<len;j++)
        {
            if(mp[s[j].first]==0)
                mp[s[j].first]=ccnt++;
            dp[i][mp[s[j].first]]+=dp[i-1][mp[s[j].second]];
        }
        if(mp[i]==0)
            mp[i]=ccnt++;
        dp[i][mp[i]]+=1;
    }
    //cout << dp[1][mp[1]] <<endl;
    //cout << dp[2][mp[2]] <<endl;
    //cout << dp[3][mp[6]] <<endl;
    //cout << dp[4][mp[4]] <<endl;

    int t;
    scanf("%d",&t);
    for(int cnt=1;cnt<=t;cnt++)
    {
        long long int n,m;
        long long int ans=0;
        scanf("%lld%lld",&n,&m);
        for(it=mp.begin();it!=mp.end(),it->first<m;it++)
        {
            if(it->second!=0)
            ans+=dp[n][it->second];
        }
        long long int sum=1;
        for(int i=0;i<n;i++)
            sum*=2;
        printf("Case #%d: %lld\n",cnt,sum-ans-1);
    }
}


猜你喜欢

转载自blog.csdn.net/xuejye/article/details/81057092
今日推荐