HDU-4028 the time of a day (discrete + dp)

Source: Internet
Author: User

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 got Ted 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

 35 510 110 128 
 

Sample output

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

Question: Give a number ranging from 1 to n and ask how many subsets of LCM are greater than or equal to M.

Idea: m is very large, and the subset also has 2 ^ 40, but it is found that the minimum public multiple range is only more than 40 thousand, therefore, we can use DP [I] [J] to indicate that the minimum public multiples of the first I count are the number of J schemes, and the results of the minimum public multiples of discrete processing save space.

#include <iostream>#include <cstdio>#include <cstring>#include <map>#include <algorithm>//typedef long long ll;typedef __int64 ll;using namespace std;map<ll, ll> dp[50];ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a%b);}ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}void init() {dp[1][1] = 1;map<ll, ll>::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(i, it->first)] += it->second;}}int main() {init();int t, cas = 1;scanf("%d", &t);int n;ll m;while (t--) {scanf("%d%I64d", &n, &m);ll ans = 0;map<ll, ll>::iterator it;for (it = dp[n].begin(); it != dp[n].end(); it++)if (it->first >= m)ans += it->second;printf("Case #%d: %I64d\n", cas++, ans);}return 0;}




HDU-4028 the time of a day (discrete + dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.