Multiple Gift(AtCoder-3731)
Problem Description
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.
Example
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~y 范圍內構造一個序列,要求第 a[i+1] 個數是第 i 個數的倍數,求序列最大的可能的長度
思路:
通過題意可知,按:x、2x、4x、8x、16x、...、2^(i-1) x 的規則構造的序列一定是最長的
因此,只要枚舉 i 求出第一個大于?2^(i-1) x 的數時,i-1 就是答案
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #include<bitset> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 8000+5; const int dx[] = {-1,1,0,0,-1,-1,1,1}; const int dy[] = {0,0,-1,1,-1,1,-1,1}; using namespace std;int main(){LL x,y;scanf("%lld%lld",&x,&y);for(LL i=0;i<100;i++){LL temp=pow(2,i-1)*x;if(temp>y){printf("%lld\n",i-1);break;}}return 0; }?
總結
以上是生活随笔為你收集整理的Multiple Gift(AtCoder-3731)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++语言基础 —— STL —— 算法
- 下一篇: 埃及分数(信息学奥赛一本通-T1444)