BestCoder Round #60 1002
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.
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≤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.
For each test case,output an answer.
3
1 1
1 2
2 4
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-;
typedef unsigned long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
unsigned int n,tt,cnt,k;
ll m;
int a[MAXN];
int prime[MAXN+];
void getPrime()
{
memset(prime,,sizeof(prime));
for(int i = ;i <= MAXN;i++)
{
if(!prime[i])prime[++prime[]] = i;
for(int j = ;j <= prime[] && prime[j] <= MAXN/i;j++)
{
prime[prime[j]*i] = ;
if(i % prime[j] == )break;
}
}
}
long long factor[][];
int fatCnt;
int getFactors(long long x)
{
fatCnt = ;
long long tmp = x;
for(int i = ; prime[i] <= tmp/prime[i];i++)
{
factor[fatCnt][] = ;
if(tmp % prime[i] == )
{
factor[fatCnt][] = prime[i];
while(tmp % prime[i] == )
{
factor[fatCnt][] ++;
tmp /= prime[i];
}
fatCnt++;
}
}
if(tmp != )
{
factor[fatCnt][] = tmp;
factor[fatCnt++][] = ;
}
return fatCnt;
} long long factor2[][];
int fatCnt2;
int getFactors2(long long x)
{
fatCnt2 = ;
long long tmp = x;
for(int i = ; prime[i] <= tmp/prime[i];i++)
{
factor2[fatCnt2][] = ;
if(tmp % prime[i] == )
{
factor2[fatCnt2][] = prime[i];
while(tmp % prime[i] == )
{
factor2[fatCnt2][] ++;
tmp /= prime[i];
}
fatCnt2++;
}
}
if(tmp != )
{
factor2[fatCnt2][] = tmp;
factor2[fatCnt2++][] = ;
}
return fatCnt2;
}
int main()
{
int i;
getPrime();
scanf("%d",&tt);
while(tt--)
{
scanf("%d %I64d",&n,&m);
getFactors(n);
bool flag=;
if(m<n)
{
printf("-1\n");
continue;
}
for(i=;i<fatCnt;i++)
{
int tot=;
while(m%factor[i][]==)
{
m/=factor[i][];
factor2[i][]=factor[i][];
factor2[i][]=tot++;
}
}
if(m!=)
{
flag=;
}
int Max=;
for(i=;i<fatCnt;i++)
{
int temp=factor[i][];
int tot=;
while(temp<factor2[i][])
{
temp<<=;
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==) return d;
return gcd(e,d%e);
}
int main()
{
freopen("stdin.txt","r",stdin);
scanf("%d",&f);
for(i=;i<=f;i++)
{
scanf("%I64d %I64d",&n,&m);
if(m%n!=){cout<<"-1\n";continue;}
if(m==n){cout<<"0\n";continue;}
if(n==){cout<<"-1\n";continue;}
else
{
m/=n;
n*=n;
j=;
while()
{
if(m==){cout<<j<<endl;break;}
t=gcd(n,m);cout<<t<<"* ";
if(m!=&&t==){cout<<"-1\n";break;}
n*=n;
m/=t;
j++;
}
}
j=n=m=;
t=;
}
return ;
}
WA
BestCoder Round #60 1002的更多相关文章
- 暴力+降复杂度 BestCoder Round #39 1002 Mutiple
题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...
- 矩阵快速幂---BestCoder Round#8 1002
当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n) f(n-1)] = [f(n ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- Manacher BestCoder Round #49 ($) 1002 Three Palindromes
题目传送门 /* Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间 ...
- 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- HDU 5505 - BestCoder Round #60 - GT and numbers
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002 思路 : N有若 ...
- BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)
今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...
- BestCoder Round #92 1002 Count the Sheep —— 枚举+技巧
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1002 题解: 做题的时候只是想到 ...
随机推荐
- [USACO2005][POJ2226]Muddy Fields(二分图最小点覆盖)
题目:http://poj.org/problem?id=2226 题意:给你一个字符矩阵,每个位置只能有"*"或者“.",连续的横着或者竖的“*"可以用一块木 ...
- Graphics samples2
为图形填充渐变色: Graphics2D g2=(Graphics2D)g; GradientPaint gra=new GradientPaint(20, 20, Color.BLUE, 100,8 ...
- 传说中的WeixinJSBridge和微信rest接口
直接上图,金山的APP“微信导航”,从界面上看有粉丝数等关键数据,实现了直接关注功能,莫不是rest接口?这江湖是大佬们的江湖,小喽啰只有眼馋的份咯. 很早就听说过WeixinJSBridge,不过官 ...
- u1-nav-html
<header id="masthead" class="masthead" role="banner"> <nav cl ...
- JQuery学习(1)
JQuery学前准备 JQuery的各种包: 1.jquery-ui(包含小工具及组件) 2.jquery-1.7.1.intellisense.js(智能提示包) 3.jquery-1.7.1.js ...
- 【Gym 100971J】Robots at Warehouse
题意 链接给你一个n*m的地图,'#'代表墙,‘.’代表可走的,1代表1号机器人,2代表2号机器人,机器人可以上下左右移动到非墙的位置,但不能走到另一个机器人身上.问能否交换1和2的位置. 分析 如果 ...
- Oracle写函数读写日志实例
1.用DBA登录赋权限create or replace directory D_OUTPUT as 'D:\TEMP'; grant read,write on directory D_OUTPUT ...
- BZOJ-2326 数学作业 矩阵乘法快速幂+快速乘
2326: [HNOI2011]数学作业 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1564 Solved: 910 [Submit][Statu ...
- Spring JdbcTemplate 的使用与学习
JDBCTemplate 是SPRING 框架自带的一种对sql 语句查询的封装 ,封装非常完善,虽然与Hibernate比起来有一点麻烦,但是学号JDBCTemplate可以让我们用Spirngmv ...
- Docker Architecture、Docker Usage
目录 . 引言 - 为什么要有Docker技术 . Docker简介 . Docker安装.部署.使用 . Docker安全 . Docker底层实现 . Docker网络配置 . Dockerfil ...