1 条题解

  • 0
    @ 2024-12-24 9:49:41

    C :

    #include<stdio.h>
    
    const int maxn=1000000007;
    
    int main(void)
    {
        long long x, m;
        int cnt;
        int temp;
        while(scanf("%lld", &m)!=EOF){
            cnt=0; temp=0;
            x=m;
        for(int i=0; i<=100000; i++)
        {
            if(x%maxn==0)
            {
                temp=1;
                break;
            }
            else
            {
                x=(8*x+7)%maxn;
                cnt++;
            }
        }
        if(temp==0)
        {
            x=m;
            cnt=0;
            x=(4*x+3)%maxn;
            cnt++;
            for(int i=1; i<=100000; i++)
            {
                if(x%maxn==0)
                {
                    temp=1;
                    break;
                }
                else
                {
                    x=(8*x+7)%maxn;
                    cnt++;
                }
            }
        }
        if(temp==0)
        {
            x=m;
            cnt=0;
            x=(4*x+3)%maxn;
            x=(4*x+3)%maxn;
            cnt+=2;
            for(int i=2; i<=100000; i++)
            {
                if(x%maxn==0)
                {
                    temp=1;
                    break;
                }
                else
                {
                    x=(8*x+7)%maxn;
                    cnt++;
                }
            }
        }
        if(temp==0)
        printf("-1\n");
        else
        printf("%d\n", cnt);
    }
        return 0;
    }
    
    

    C++ :

    #include<stdio.h>
    
    const int maxn=1000000007;
    
    int main(void)
    {
        long long x, m;
        int cnt;
        int temp;
        while(scanf("%lld", &m)!=EOF){
            cnt=0; temp=0;
            x=m;
        for(int i=0; i<=100000; i++)
        {
            if(x%maxn==0)
            {
                temp=1;
                break;
            }
            else
            {
                x=(8*x+7)%maxn;
                cnt++;
            }
        }
        if(temp==0)
        {
            x=m;
            cnt=0;
            x=(4*x+3)%maxn;
            cnt++;
            for(int i=1; i<=100000; i++)
            {
                if(x%maxn==0)
                {
                    temp=1;
                    break;
                }
                else
                {
                    x=(8*x+7)%maxn;
                    cnt++;
                }
            }
        }
        if(temp==0)
        {
            x=m;
            cnt=0;
            x=(4*x+3)%maxn;
            x=(4*x+3)%maxn;
            cnt+=2;
            for(int i=2; i<=100000; i++)
            {
                if(x%maxn==0)
                {
                    temp=1;
                    break;
                }
                else
                {
                    x=(8*x+7)%maxn;
                    cnt++;
                }
            }
        }
        if(temp==0)
        printf("-1\n");
        else
        printf("%d\n", cnt);
    }
        return 0;
    }
    
    

    Java :

    import java.util.*;
    
    public class Main{
        public static long mod=1000000007;
        public static long fun(long x){
            return (2*x+1)%mod;
        }
        public static void main(String args[]){
            Scanner cin=new Scanner(System.in);
            long x,n;
            long mx=100001*3;
            while(cin.hasNext()){
                x=cin.nextInt();
                n=0;
                while(x!=0 && n<mx){
                    x=fun(x);
                    n++;
                }
                if(mx==n){
                    System.out.println(-1);
                }
                else{
                    long xx=n/3,yy;
                    n%=3;
                    while(n%2!=0){
                        xx--;
                        n+=3;
                    }
                    yy=n/2;
                    xx+=yy;
                    if(xx>100000)
                        System.out.println(-1);
                    else
                        System.out.println(xx);
                }
           }
        }
    }
    
    • 1

    信息

    ID
    989
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者