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 .
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 .
T≤1000 , 1≤N≤1000000 , 1≤M≤263 .
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.
In the next T lines there are two numbers N and M .
T≤1000 , 1≤N≤1000000 , 1≤M≤263 .
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;
}