Arcane Numbers 1

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Vance and Shackler like playing games. One day, they are playing a game called "arcane numbers". The game is pretty simple, Vance writes down a finite decimal under base A, and then Shackler translates it under base B. If Shackler can translate it into a finite decimal, he wins, else it will be Vance’s win. Now given A and B, please help Vance to determine whether he will win or not. Note that they are playing this game using a mystery language so that A and B may be up to 10^12.
 

Input

The first line contains a single integer T, the number of test cases. 
For each case, there’s a single line contains A and B. 
 

Output

For each case, output “NO” if Vance will win the game. Otherwise, print “YES”. See Sample Output for more details.
 

Sample Input

3
5 5
2 3
1000 2000
 

Sample Output

Case #1: YES
Case #2: NO
Case #3: YES

#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
ll a , b ;
int main () {
int T ;
scanf ("%d" , &T ) ;
for (int cas = 1 ; cas <= T ; cas ++) {
scanf ("%I64d%I64d" , &a , &b) ;
ll g = __gcd (a,b) ;
if (g == 1) {
printf ("Case #%d: NO\n" , cas) ;
continue ;
}
a/= g ;
ll flag = __gcd (a , g) ;
while (flag != 1) {
while (a % flag == 0) a/=flag ;
flag = __gcd (a , g) ;
}
if (a == 1 ) printf ("Case #%d: YES\n" , cas) ;
else printf ("Case #%d: NO\n" , cas) ;
}
return 0 ;
}

  其实用O(sqrt(n))的方法也是可以的,但姿势不好看很容易tle。

用log(n)的方法理解上也很容易,先求g = gcd(a , b) ,那么显然若 {a的质因子 } 属于 {g的质因子} , a^k % b 一定等于0 ------no.1

所以之后不断求 flag = gcd (g , a) , 并让 a /= flag ;

最终a 和 g互质时,若a != 1 , 则no.1肯定不成立,那么肯定不存在k,能让所求等式成立,

反之a = 1的情况,就肯定成立了。

2012多校3.A(用O(log(n))判断b^k % a == 0)的更多相关文章

  1. Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化

    题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$  $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...

  2. HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

    Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K ...

  3. 牛客多校第十场 H Stammering Chemists 判断图同构

    题意: 给出一个无向图,表示一种有机物质的结构式,问你这个有机物质是列表中的哪个. 题解: 判断图同构需要枚举全排列以对应点,但是此题中几乎只需要将点度数排序后一个一个比较,对于甲基位置再加个特判即可 ...

  4. Linux system log avahi-daemon[3733]: Invalid query packet

    在检查Linux的日志文件时,发现大量 avahi-daemon[3733]: Invalid query packet错误(不同服务器对应的数字有所不同) Aug  3 07:00:01 hostn ...

  5. [转]Android输出Log到文件

    前言:开发中遇到mx4这款机型Eclipse联调不上,logcat看不了,需要输出生成文件查看调试信息.网上搜了下,功能很完善了.startService和过滤输出信息需要自己添加设置,另外注意添加权 ...

  6. SQL 2008 RAISERROR语法在SQL 2012/2014不兼容问题

    原文 旧的RAISERROR语法在SQL 2012不兼容问题 raiserror 写法: SQL 2008: raiserror 55030 'text error' SQL 2012: raiser ...

  7. Mysql query log

    一.查询日志的概念: 查询日志记录MySQL中所有的query,通过"--log[=file_name]"来打开该功能.由于记录了所有的query,包括所有的select,体积比较 ...

  8. [转] C#实现自动化Log日志

    qing2005原文地址 C#实现自动化Log日志 在开发项目的时候,我们不免要使用Log记录日志,使用最多的是Log4Net和EntLib Log,在需要记录日志的代码处加入log.Write(日志 ...

  9. SQL Server 2012 Express LocalDB

    微软最新推出的 SQL Server 2012 Express LocalDB 是一种 SQL Server Express 的运行模式,特别适合用在开发环境使用,也内置在 Visual Studio ...

随机推荐

  1. POJ 1236 Network of Schools(强连通分量/Tarjan缩点)

    传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...

  2. HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)

    题目链接: 传送门 畅通工程续 Time Limit: 1000MS     Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...

  3. POJ - Til the Cows Come Home(Dijkstra)

    题意: 有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 分析: 典型的模板题,但是一定要注意有重边,因此需要对输入数据加以判断,保存较短的边,这样才能正确使用模板. ...

  4. Linux tcpdump 命令详解

    简介用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的& ...

  5. Python基本数据类型之tuple

    一.创建元组: ages = (11, 22, 33, 44, 55) ages = tuple((11, 22, 33, 44, 55)) 元组和列表几乎一样 元组的元素不可修改,但是元组元素的元素 ...

  6. POJ3254Corn Fields(状态压缩DP入门)

    题目链接 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一 ...

  7. 真机调试之android手机+chrome

    真机调试之android手机+chrome 虽然chrome上的移动设备模拟器很强大,但是在真机运行的时候,总会遇到一些小问题,这时就需要使用真机调试了. 第一步:准备一台android手机,并在手机 ...

  8. BZOJ3685: 普通van Emde Boas树

    显然这题的所有操作都可以用set,但是直接用set肯定要T,考虑到读入量较大,使用fread读入优化,就可以卡过去了. #include<bits/stdc++.h> using name ...

  9. phpcms后台获取当前登录账号的数据

    $amdinid=$_SESSION['userid'];$infoadmin=$this->admin->get_one(array('userid'=>$amdinid)); v ...

  10. e_msg_c_as_register_req-注册存储过程

    TOP:BEGIN #Routine body goes here... IF EXISTS ( SELECT * FROM `global_account` WHERE `plantform_id` ...