H. Hard math problem



H. Hard math problem

Time Limit: 2000ms
Case Time Limit: 2000ms
Memory Limit: 65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Submit  Status  PID: 29363
Font Size:  +  -
    Xioumu was trapped by a mathematical puzzle recently.
 
    He wrote the number 0,1,2 on the blackboard, and then he does lots of operations on them, in each operation, he took out the larger two numbers of the three every time, added the two numbers and wrote down the sum on the blackboard, and then wiped any one of the original three numbers. He needed to use a minimum times of operations to get a maximum number x. Now he needs your help. 

Input

    The first line is an integer T indicates the number of test cases.

    Input contains several test cases. 

    For each test case, there is a number x (1<=x<=5*10^5) in a line.

Output

  For each case, output the case number first. Then output the minimum number of operations. If he can’t get x, just output -1.

Sample Input

3
2
6
13

Sample Output

Case 1: 0
Case 2: 4
Case 3: 4  



#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int ans;

int fff(int a,int b)
{
    ans++;
    if(a<1||b<2) return 0x3f3f3f3f;
    if(ans>=100) return 0x3f3f3f3f;
    if(a==1&&b==2) return ans;
    b=b-a;
    if(b<a)
        swap(a,b);
    fff(a,b);
}

int main()
{
    int T;
    cin>>T;
    int cas=1;
while(T--)
{
    int n;
    cin>>n;
    int cnt=0x3f3f3f3f;
    for(int i=1;i<=n/2;i++)
    {
        ans=0;
        int a=i;int b=n-i;
        if(a>b) swap(a,b);
        cnt=min(cnt,fff(a,b));
    }
    if(cnt>=0x3f3f3f3f&&n!=2) cnt=-1;
    if(n==2) cnt=0;
    printf("Case %d: %d\n",cas++,cnt);
}
    return 0;
}

转载于:https://www.cnblogs.com/CKboss/p/3350916.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值