hdu5505

GT and numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1003    Accepted Submission(s): 263


Problem Description
You are given two numbers  N  and  M .

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

Work out how many steps can  N  be equal to  M  at least.

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

Input
In the first line there is a number  T . T  is the test number.

In the next  T  lines there are two numbers  N  and  M .

T1000 1N1000000 , 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
 

参考链接:
官方题解
http://www.cnblogs.com/A123456A/archive/2013/01/25/2876634.html(不同数据类型 scanf 时用什么,比如 int ,用 %d )

这道题一开始想成了以 num[i][1] (含义见代码)为公比的等比数列,后来又想成了以 num[i][1] 为公差的等差数列,最后终于推出来了是以 2 为等比的等比数列,但是却想成了是这个等比数列的和是最后某个质因数的幂次,看了题解后,才反应过来是数列中的最后一个数。
还有一个 2^63 这个坑。
(最近听 《两小无猜》(陈秋实&蔡照)听上瘾了,同时对队友进行了洗脑式重复。 生气 )

已ac的代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
#define N 20

long long int num[N][3];

int main(){
    int t;
    long long int n;
    unsigned long long int m;

    scanf("%d",&t);
    while(t--){
        scanf("%lld%llu",&n,&m);

        num[0][0]=0;
        for(int i=2;i*i<=n;i++){
            if(n%i==0){
                num[0][0]++;
                num[num[0][0]][0]=i;
                num[num[0][0]][1]=0;

                while(n%i==0){
                    num[num[0][0]][1]++;
                    n=n/i;
                }
                //printf("%d %d\n",i,num[num[0][0]][1]);
            }
        }
        if(n!=1){
            num[0][0]++;
            num[num[0][0]][0]=n;
            num[num[0][0]][1]=1;
            //printf("%d %d\n",n,num[num[0][0]][1]);
        }

        bool pd=true;
        unsigned long long int tempm=m;
        for(int i=1;i<=num[0][0];i++){
            long long int tempnum=num[i][0];

            num[i][2]=0;
            while(tempm%tempnum==0){
                num[i][2]++;
                tempm=tempm/tempnum;
            }
            if(num[i][2]<num[i][1]){
                pd=false;
                break;
            }
        }
        if(tempm!=1||pd==false){
            printf("%d\n",-1);
        }
        else{
            long long int maxcou=0;
            long long int temp;

            for(int i=1;i<=num[0][0];i++){//是公比为2的等比数列
                double tempcou=(double)num[i][2]/num[i][1];

                tempcou=log2(tempcou);
                temp=(long long int)tempcou;

                if(temp!=tempcou){
                    temp++;
                }

                if(i==1){
                    maxcou=temp;
                }
                else if(maxcou<temp){
                    maxcou=temp;
                }
            }

            printf("%lld\n",maxcou);
        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值