More than 2012 Schools 3. A (using O (log (n)) to determine b^k% A = = 0)

Source: Internet
Author: User

Arcane Numbers 1Time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 4320

Description

Vance and Shackler like the playing games. One day, they is 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'll be Vance ' s win. Now given A and B, please help Vance to determine whether he'll win or not. Note that they is playing this game using a mystery language so, A and B May is 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'll win the game. Otherwise, print "YES". See Sample Output for more details.

Sample Input

35 52 31000 2000

Sample Output

Case #1: yescase #2: nocase #3: YES
#include <bits/stdc++.h>using namespace std;typedef a 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;}

In fact, using O (sqrt (n)) method is also possible, but the posture is not good-looking very easy tle.

It is also easy to understand the method of log (n), first ask for g = gcd (A, B), then obviously if {A's quality factor} belongs to {G's quality factor}, A^k% B must be equal to 0------

The flag is then constantly asked for (G, a) and gcd a/= flag;

Finally A and G coprime, if a! = 1, then the most certainly does not hold, then certainly does not exist K, can let the equation set up,

Conversely a = 1 of the situation, it must be set up.

More than 2012 Schools 3. A (using O (log (n)) to determine b^k% A = = 0)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.