hdu 4028 The time of a day(离散化dp)

    xiaoxiao2025-03-18  11

    题目链接

    The time of a day

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1268    Accepted Submission(s): 571 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 The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest

    题目描述有点生涩,不过看出来了就是:给你n个数(1-n),让你从中选出一些数,使这些数的最小公倍数大于等于m,问共有多少种选择。

    分析:

    dp[i][j]表示前i个指针中选取方案状态(最小公倍数)为j有多少种那么方程为dp[i][j]=dp[i-1][j](不选第i)+dp[i-1][k](选第i个 只有当lcm(k,i)==j才加)。

    可是第二维不连续,有很大的空缺,那么此时可以离散化处理,使用map解决,详见代码:

    #include<iostream> #include<cstdio> #include<algorithm> #include<map> using namespace std; typedef long long ll; const int maxn=40+5; map<ll,ll> ans[maxn]; ll gcd(ll a,ll b) { return a%b==0? b:gcd(b,a%b); } ll lcm(ll a,ll b) { return a*b/gcd(a,b); } void init() { ans[1][1]=1; for(ll i=2;i<=40;i++) { ans[i]=ans[i-1]; ans[i][i]+=1; //注意这个初始化! map<ll,ll>::iterator it; for(it=ans[i-1].begin();it!=ans[i-1].end();++it) { ll p=lcm(i,it->first); ans[i][p]+=it->second; } } } int main() { int cas; scanf("%d",&cas); init(); for(int k=1;k<=cas;k++) { ll s,t; scanf("%I64d%I64d",&s,&t); ll res=0; for(map<ll,ll>::iterator it=ans[s].begin();it!=ans[s].end();++it) { if(it->first>=t) res+=it->second; } printf("Case #%d: %I64d\n",k,res); } }

    转载请注明原文地址: https://ju.6miu.com/read-1297138.html
    最新回复(0)