HDU 5505 GT and numbers(对于被我无意识坑到的3个人我表示抱歉)——BestCoder Round #60

来源:互联网 发布:网络传输使用那些协议 编辑:程序博客网 时间:2024/05/29 11:13

GT and numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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.

T10001N1000000,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
31 11 22 4
 

Sample Output
0-11
 

Source
BestCoder Round #60
 

/************************************************************************/

附上该题对应的中文题

GT and numbers

 
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
给出两个数NNMMNN每次可以乘上一个自己的因数变成新的NN。求最初的NNMM至少需要几步。如果永远也到不了输出-11
输入描述
第一行读入一个数TT表示数据组数。接下来TT行,每行两个数NNMMT\leq1000T1000, 1\leq N \leq 10000001N1000000,1 \leq M \leq 2^{63}1M263.注意M的范围。hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。
输出描述
对于每组数据,输出一个数表示答案。
输入样例
31 11 22 4
输出样例
0-11
/****************************************************/

出题人的解题思路:

1002 GT and numbers

如果AA大于BB那么显然无解。

考虑把AABB分解质因数。

BB存在AA没有的质因数也显然无解。

对于某一个AA的质因数的次数。为了加速接近BB,它一定是每次翻倍,最后一次的时候把剩下的加上。

那么答案就是最小的kk使得2^{k}*A_{num} \geq B_{num}2kAnumBnum

最后把每个质因数的答案max起来即可。

感觉本场比赛没有trick(雾~),我就打算加入一个很经典的trick:

BB可以刚好等于2^{63}263,这样就要开unsigned long long。

同时我还在题目里特别提醒了“注意M的范围”

可惜仍然有很多选手没有注意。

这里我表示歉意,也希望你们以后可以更加仔细一点。

对于题目的解法,我相信通过出题人的解题报告,多多少少都能明白一点,在此,我也不多说,有不明白的可以留下评论
在此,其实我想说的是,B的定义问题,因为B可以取到263,所以不管是long long,还是__int64都是不够大的,因为它们所能表示的最大值为263-1,所以就如上面的题解所说要用到unsigned,也就是这个原因,我不小心坑害了3个人
可能是看到我用了__int64吧,他们果断用下面这组数据来hack我
1
2 9223372036854775808

然而令我也没想到的是居然还是对的,虽然__int64的变量m无法保存263,但是它会保存成-263,这对于求公约数是没有影响的,而我代码中的n不断变大,就算m为-263,n最终也能成-263,这样就保证了可以判断n==m
orz,不小心坑害了你们吐舌头

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 1000000;const int inf = 1000000000;const int mod = 2009;__int64 gcd(__int64 a,__int64 b){    if(a%b)        return gcd(b,a%b);    return b;}int main(){    int t,i,k,ans;    __int64 n,m;    scanf("%d",&t);    while(t--)    {        ans=0;        scanf("%I64d%I64d",&n,&m);        while(n!=m)        {            if(m%n)            {                puts("-1");                break;            }            k=gcd(m/n,n);            if(k==1)            {                puts("-1");                break;            }            n*=k;            ans++;        }        if(n==m)            printf("%d\n",ans);    }    return 0;}
菜鸟成长记

1 0
原创粉丝点击