2012多校3.A(用O(log(n))判断b^k % a == 0)

本文介绍了名为ArcaneNumbers的游戏算法实现细节。玩家通过转换不同进制下的有限小数来决定胜负。文章提供了完整的代码示例,并详细解释了算法背后的逻辑,包括如何使用最大公约数(gcd)进行关键步骤的计算。
Arcane Numbers 1
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 4320

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的情况,就肯定成立了。

转载于:https://www.cnblogs.com/get-an-AC-everyday/p/4758713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值