C - Multiple Gift
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
As a token of his gratitude, Takahashi has decided to give his mother an integer sequence. The sequence A needs to satisfy the conditions below:
- A consists of integers between X and Y (inclusive).
- For each 1≤i≤|A|−1, Ai+1 is a multiple of Ai and strictly greater than Ai.
Find the maximum possible length of the sequence.
Constraints
- 1≤X≤Y≤1018
- All input values are integers.
Input
Input is given from Standard Input in the following format:
X Y
Output
Print the maximum possible length of the sequence.
Sample Input 1
3 20
Sample Output 1
3
The sequence 3,6,18 satisfies the conditions.
Sample Input 2
25 100
Sample Output 2
3
Sample Input 3
314159265 358979323846264338
Sample Output 3
31
题目大意:构造一个数列,要求所有项都在[x,y]之间,要求每一项必须能整除之前的所有项。
思路:一开始我想的模拟,第一个数很明显就是x,然后选择最后一个数去看能不能整除前面的,如果可以那么就把最后一个数*2当作新的数,否则就*剩下没能整除的部分。然后爆炸,siy死也过不了第一个样例。
后来选择找规律。log2((r-l)/l)+1,不过,WA18,就开始“调参”,log2((r-l+1)/l)+1,log2((r-l)/l+1)+1......
总是WA18让我觉得有点不大对劲。
手动求log。
A了。
貌似是精度的问题。
以后再也不log了。
附代码。
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pause system("pause")
using namespace std;
inline ll read()
{
long long f=1,sum=0;
char c=getchar();
while (c<'0' || c>'9')
{
if (c=='-') f=-1;
c=getchar();
}
while (c>='0' && c<='9')
{
sum=sum*10+c-'0';
c=getchar();
}
return sum*f;
}
int main()
{
ll l=read(),r=read();
int ans=0;
while (r>=l) l*=2,ans++;
cout<<ans;
return 0;
}