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
  • 相关阅读:
    【HDU3721】枚举+最长路
    满足要求的最长上升子序列(nlogn)
    Flask入门 表单Flask-wtf form原生 Bootstrap渲染(七)
    Flask入门之模板导入与块宏(六)
    Flask入门模板过滤器与测试器(五)
    Flask入门模板Jinja2语法与函数(四)
    Flask入门flask-script 蓝本 钩子函数(三)
    Flask入门request session cookie(二)
    Flask入门 flask结构 url_for 重定向(一)
    Django问卷调查项目思路流程
  • 原文地址:https://www.cnblogs.com/dzzy/p/4888403.html
Copyright © 2011-2022 走看看