• HDU 4028 The time of a day (数论,离散DP)


    The time of a day

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


    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
     
    Recommend
    lcy
     
     
    离散DP。
    就是求1-n中任意选取若干个数的LCM
     
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<iostream>
    #include<map>
    using namespace std;
    
    map<long long,long long>dp[45];
    
    long long gcd(long long a,long long b)
    {
        if(b==0)return a;
        return gcd(b,a%b);
    }
    long long lcm(long long a,long long b)
    {
        return a*b/gcd(a,b);
    }
    
    int main()
    {
        dp[1][1]=1;
        map<long long,long long>::iterator it;
        for(int i=2;i<=40;i++)
        {
            dp[i]=dp[i-1];
            dp[i][i]++;
            for(it=dp[i-1].begin();it!=dp[i-1].end();it++)
            {
                dp[i][lcm(it->first,i)]+=it->second;
            }
        }
        int T;
        int iCase=0;
        scanf("%d",&T);
        int n;
        long long m;
        while(T--)
        {
            iCase++;
            scanf("%d%I64d",&n,&m);
            long long ans=0;
            for(it=dp[n].begin();it!=dp[n].end();it++)
              if(it->first>=m)
                ans+=it->second;
            printf("Case #%d: %I64d\n",iCase,ans);
        }
        return 0;
    }
  • 相关阅读:
    Java入门——(3)面对对象(下)
    Java入门——(8)网络编程
    Java入门——(2)面对对象(上)
    MAC下的Intellij IDEA常用快捷键
    RedHat安装yum+配置国内yum源
    XGBoost算法
    Bagging和Boosting 概念及区别
    关于python的sort和sorted
    sklearn中常用数据预处理方法
    安装Scala
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2653278.html
Copyright © 2020-2023  润新知