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. ExceptionLess异常日志收集框架-1

    哈哈,中秋和代码更配哦,不知不觉一年过半了,祝园友们中秋快乐 前一阵子在博客园看到了一篇博文 http://www.cnblogs.com/savorboard/p/exceptionless.htm ...

  2. 更改primefaces theme

    PrimeFaces is using jQuery ThemeRoller CSS theme framework, and come with 30+ pre-designed themes th ...

  3. Spring data jpa批量插入和更新

    http://blog.csdn.net/wangshfa/article/details/27323297   1 public interface BatchDao<T> { 2    ...

  4. HTML5 history

    引入history.pushState的来龙去脉 大家都知道web2.0以来,大家都喜欢使用ajax来请求数据,提高用户体验,但是传统的ajax可以无刷新改变页面内容,但无法改变页面URL,无刷新的改 ...

  5. 【百度百科】对焦Focus

    词语解释 duìjiāo [focusing] 指使用照相机时调整好焦点距离 对焦也叫对光.聚焦.通过照相机对焦机构变动物距和相距的位置,使被拍物成像清晰的过程就是对焦. 自动对焦 传统相机,采取一种 ...

  6. SaltStack项目实战(七)

    上文 http://www.cnblogs.com/shhnwangjian/p/6027992.html 四.memcached 1)创建www用户 mkdir -p /srv/salt/prod/ ...

  7. 【原】react中如何使用jquery插件

    react的思想是虚拟dom,提倡最好较少dom的操作,可是我们在写网页的时候,有些复杂的交互还是离不开jquery插件的.而且当你把jquery直接拿来用的时候,你会发觉会报错,要么是找不到那个插件 ...

  8. spring-data-jpa Repository的基本知识

    1.项目中的Repository对象的使用 2.Repository 引入的两种方式 继承和使用注解 3.Repository接口的定义 Repository 接口是 spring Data 的一个核 ...

  9. select例子

    好长时间没有写了,其实一直在坚持学习. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h& ...

  10. 基础SQL语句

    SQL语句: 1.插入 方法一: "INSERT INTO [DB].[dbo].[T_Table] ([ID],[Name],[Amount],[Creater],[CreatedOn], ...