The time of a day
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 1383 Accepted Submission(s): 637
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.
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)
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
题意:
从1-N中选择任意的数,使他们的最小公倍数大于等于M,问有多少种方案。
思路:
设dp[i][j]表示从1到i中取出的数使他们的最小公倍数为j的个数。
很明显状态转移方程为
dp[i+1][lcm(i+1,j)]+=dp[i][j]和dp[i+1][j]+=dp[i][j]。
但是发现1到40的最小公倍数非常的大,所以开不出这么大的数组,实际上我们也不需要开这么大的数组,有些状态是无效的。
所以我们考虑离散化,用map来存下这些最小公倍数j,我们只需要搜寻这些有效的状态并转移就行了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
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;
}
map<ll,ll> dp[41];
map<ll,ll>:: iterator it;
int main()
{
int t,cas=1,n;
ll m;
for(int i=1;i<40;i++)
{
dp[i][i]++;
for(it=dp[i].begin();it!=dp[i].end();it++)
{
dp[i+1][lcm((ll)(i+1),it->first)]+=dp[i][it->first];
dp[i+1][it->first]+=dp[i][it->first];
}
}
scanf("%d",&t);
while(t--)
{
scanf("%d%lld",&n,&m);
ll ans=0;
for(it=dp[n].begin();it!=dp[n].end();it++)
{
if(it->first>=m)
ans+=it->second;
}
printf("Case #%d: ",cas++);
printf("%lld\n",ans);
}
return 0;
}