zoukankan      html  css  js  c++  java
  • BestCoder Round #60 1002

    Problem Description

    You are given two numbers NNN and MMM.

    Every step you can get a new NNN in the way that multiply NNN by a factor of NNN.

    Work out how many steps can NNN be equal to MMM at least.

    If N can't be to M forever,print −1-11.

    Input

    In the first line there is a number TTT.TTT is the test number.

    In the next TTT lines there are two numbers NNN and MMM.

    T≤1000Tleq1000T1000, 1≤N≤10000001leq N leq 10000001N1000000,1≤M≤2631 leq M leq 2^{63}1M263​​.

    Be careful to the range of M.

    You'd better print the enter in the last line when you hack others.

    You'd better not print space in the last of each line when you hack others.

    Output

    For each test case,output an answer.

    Sample Input
    3
    1 1
    1 2
    2 4
    Sample Output
    0
    -1
    1

    23333这道题也是够了,按题意拍也是能A的,但是一开始的想法一直WA,感觉好坑取 循环取n,m的最大公约数t 然后n*n m/t更新,后来想有可能是n*n超了Long Long
    AC代码
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<map>
    using namespace std;
    #define MOD 1000000007
    const int INF=0x3f3f3f3f;
    const double eps=1e-5;
    typedef unsigned long long ll;
    #define cl(a) memset(a,0,sizeof(a))
    #define ts printf("*****
    ");
    const int MAXN=1000005;
    unsigned int n,tt,cnt,k;
    ll m;
    int a[MAXN];
    int prime[MAXN+1];
    void getPrime()
    {
        memset(prime,0,sizeof(prime));
        for(int i = 2;i <= MAXN;i++)
        {
            if(!prime[i])prime[++prime[0]] = i;
            for(int j = 1;j <= prime[0] && prime[j] <= MAXN/i;j++)
            {
                prime[prime[j]*i] = 1;
                if(i % prime[j] == 0)break;
            }
        }
    }
    long long factor[100][2];
    int fatCnt;
    int getFactors(long long x)
    {
        fatCnt = 0;
        long long tmp = x;
        for(int i = 1; prime[i] <= tmp/prime[i];i++)
        {
            factor[fatCnt][1] = 0;
            if(tmp % prime[i] == 0 )
            {
                factor[fatCnt][0] = prime[i];
                while(tmp % prime[i] == 0)
                {
                    factor[fatCnt][1] ++;
                    tmp /= prime[i];
                }
                fatCnt++;
            }
        }
        if(tmp != 1)
        {
            factor[fatCnt][0] = tmp;
            factor[fatCnt++][1] = 1;
        }
        return fatCnt;
    }
    
    long long factor2[100][2];
    int fatCnt2;
    int getFactors2(long long x)
    {
        fatCnt2 = 0;
        long long tmp = x;
        for(int i = 1; prime[i] <= tmp/prime[i];i++)
        {
            factor2[fatCnt2][1] = 0;
            if(tmp % prime[i] == 0 )
            {
                factor2[fatCnt2][0] = prime[i];
                while(tmp % prime[i] == 0)
                {
                    factor2[fatCnt2][1] ++;
                    tmp /= prime[i];
                }
                fatCnt2++;
            }
        }
        if(tmp != 1)
        {
            factor2[fatCnt2][0] = tmp;
            factor2[fatCnt2++][1] = 1;
        }
        return fatCnt2;
    }
    int main()
    {
        int i;
        getPrime();
        scanf("%d",&tt);
        while(tt--)
        {
            scanf("%d %I64d",&n,&m);
            getFactors(n);
            bool flag=1;
            if(m<n)
            {
                printf("-1
    ");
                continue;
            }
            for(i=0;i<fatCnt;i++)
            {
                int tot=1;
                while(m%factor[i][0]==0)
                {
                    m/=factor[i][0];
                    factor2[i][0]=factor[i][0];
                    factor2[i][1]=tot++;
                }
            }
            if(m!=1)
            {
                flag=0;
            }
            int Max=0;
            for(i=0;i<fatCnt;i++)
            {
                int temp=factor[i][1];
                int tot=0;
                while(temp<factor2[i][1])
                {
                    temp<<=1;
                    tot++;
                }
                Max=max(Max,tot);
            }
            if(!flag)
            {
                printf("-1
    ");
            }
            else
            {
                printf("%d
    ",Max);
            }
        }
    }
    AC
    WA
    #include<stdio.h>
    #include<iostream>
    using namespace std;
    long long t,n,m;
    int f,i,j;
    int gcd(long long d,long long e)
    {
        if(e==0) return d;
        return gcd(e,d%e);
    }
    int main()
    {
        freopen("stdin.txt","r",stdin);
        scanf("%d",&f);
        for(i=1;i<=f;i++)
        {
            scanf("%I64d %I64d",&n,&m);
            if(m%n!=0){cout<<"-1
    ";continue;}
            if(m==n){cout<<"0
    ";continue;}
            if(n==1){cout<<"-1
    ";continue;}
            else
            {
                m/=n;
                n*=n;
                j=1;
                while(1)
                {
                    if(m==1){cout<<j<<endl;break;}
                    t=gcd(n,m);cout<<t<<"* ";
                    if(m!=1&&t==1){cout<<"-1
    ";break;}
                    n*=n;
                    m/=t;
                    j++;
                }
            }
            j=n=m=0;
            t=1;
        }
        return 0;
    }
    WA
  • 相关阅读:
    十二、React 生命周期函数
    十一、React 获取服务器数据: axios插件、 fetch-jsonp插件的使用
    备份CSDN
    十、React 父组件传来值的类型控制propTypes、父组件如果不传值defaultProps
    九、React中的组件、父子组件、React props父组件给子组件传值、子组件给父组件传值、父组件中通过refs获取子组件属性和方法
    phpStudy配置多站点多域名和多端口的方法
    八、8.2自写模块,引入及使用(封装)
    八、React实战:可交互待办事务表(表单使用、数据的本地缓存local srtorage、生命同期函数(页面加载就会执行函数名固定为componentDidMount()))
    七、React表单详解 约束性和非约束性组件 input text checkbox radio select textarea 以及获取表单的内容
    索引原理与数据库优化
  • 原文地址:https://www.cnblogs.com/dzzy/p/4888403.html
Copyright © 2011-2022 走看看