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-1−1.

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≤1000T\leq1000T≤1000, 1≤N≤10000001\leq N \leq 10000001≤N≤1000000,1≤M≤2631 \leq M \leq 2^{63}1≤M≤2?63??.

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 LongAC代码

#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("*****\n");
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\n");
            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\n");
        }
        else
        {
            printf("%d\n",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\n";continue;}
        if(m==n){cout<<"0\n";continue;}
        if(n==1){cout<<"-1\n";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\n";break;}
                n*=n;
                m/=t;
                j++;
            }
        }
        j=n=m=0;
        t=1;
    }
    return 0;
}

WA

时间: 2024-10-09 19:51:53

BestCoder Round #60 1002的相关文章

Manacher BestCoder Round #49 ($) 1002 Three Palindromes

题目传送门 1 /* 2 Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 3 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间,位运算 >>1 比 /2 速度快,用了程序跑快200ms左右,位运算大法好 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1

矩阵快速幂---BestCoder Round#8 1002

当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n)   f(n-1)] = [f(n-1)    f(n-2)] [ 1 1 ]     [ 1 0 ] 设A = [ 1 1 ]  [ 1 0 ] [f(n)   f(n-1)] = [f(n-2)   f(n-3)]*A*A[f(n)   f(n-1)] = [f(2)   f(1)]*A^(n-2)矩阵满足结合律,所以先计算A^

BestCoder Round #4 1002

这题真是丧心病狂,引来今天的hack狂潮~ Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 10    Accepted Submission(s): 3 Problem Description There are N point on X-axis . Miaomiao would like t

HDU BestCoder Round #1 1002 项目管理

项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 每个节点都有一个能量值. 现在我们

hdu5504 GT and sequence(BestCoder Round #60 )

GT and sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 334 Accepted Submission(s): 85 Problem Description You are given a sequence of N integers. You should choose some numbers(at least o

贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an的值最优,每次还要和an比较 4 注意:不能选取两个相同的数 5 反思:比赛时想到了%p和sort,lower_bound,但是还是没有想到这个贪心方法保证得出最大值,还是题目做的少啊:( 6 */ 7 #include <cstdio> 8 #include <algorithm>

HDU 5281 BestCoder Round #47 1002:Senior&#39;s Gun

Senior's Gun Accepts: 235 Submissions: 977 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 学姐姐是一个酷酷的枪手. 她常常会随身携带n把枪,每把枪有一个攻击力a[i]. 有一天她遇到了m只怪兽,每只怪兽有一个防御力b[j].现在她决定用手中的枪消灭这些怪兽. 学姐姐可以用第i把枪消灭第j只怪兽当且仅当b[j]≤a[i],同时她会获

二分图判定+点染色 BestCoder Round #48 ($) 1002 wyh2000 and pupil

题目传送门 1 /* 2 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 3 每一次二分图匹配时,将点多的集合加大最后第一个集合去 4 注意:n <= 1,no,两个集合都至少有一人:ans == n,扔一个给另一个集合 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 #in

暴力+降复杂度 BestCoder Round #39 1002 Mutiple

题目传送门 1 /* 2 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 3 若没有,默认加0,nlogn复杂度: 4 我用暴力竟然水过去了:) 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <algorithm> 11 using namespace std;