zoukankan      html  css  js  c++  java
  • cf 833 A 数论

    A. The Meaningless Game
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.

    The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by k2, and the loser's score is multiplied by k. In the beginning of the game, both Slastyona and Pushok have scores equal to one.

    Unfortunately, Slastyona had lost her notepad where the history of all n games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not.

    Input

    In the first string, the number of games n (1 ≤ n ≤ 350000) is given.

    Each game is represented by a pair of scores a, b (1 ≤ a, b ≤ 109) – the results of Slastyona and Pushok, correspondingly.

    Output

    For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise.

    You can output each letter in arbitrary case (upper or lower).

    Example
    Input
    6
    2 4
    75 45
    8 8
    16 16
    247 994
    1000000000 1000000
    Output
    Yes
    Yes
    Yes
    No
    No
    Yes
    Note

    First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.

    The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3.

    题意,给出两个人的初始分值都是1,和结束分值(a,b),现在判断有没有可能通过数局游戏到达这个分值。

    规则,每次选出一个自然数k,其中一个人的分值乘上k*k,另一个人就乘上k,反之亦然。

    当时推出来式子了,却没想到怎么证明哎。

    设进行了n局游戏,则有  a*b=(k1*k2*k3......kn)3,这个并不难证明,我们假设存在整数c=k1*k2*k3....*kn使得等式成立,

    则c=cbrt(a*b),接着就要找c和a,b的关系,如果c真的存在那么a,b都能整除以c,x=a/c,y=b/c;

    如果x,y是正确的解那么代回去之后   a=x*x*y  b=y*y*x; 判断一下就好了。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define LL long long
     4 int main()
     5 {
     6     int n;
     7     LL a,b;
     8     scanf("%d",&n);
     9     while(n--){
    10         scanf("%lld%lld",&a,&b);
    11         LL c=cbrt((long double)a*b);
    12         LL x=a/c,y=b/c;
    13         if(a==x*x*y&&b==y*y*x) puts("Yes");
    14         else puts("No");
    15     }
    16     return 0;
    17 }

     上面是看的别人的其实这个思路不是很好懂,如果c存在的话,那么我们可以二分出c的值进行判定c*c*c==a*b是否成立即可,但注意这并不是充要条件,

    c还要满足 a%c==0&&b%c==0没写这两个导致我WA

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define LL long long
     4 LL solve(LL a,LL b)
     5 {
     6     LL l=0,r=1e6;
     7     while(l<r){
     8         LL mid=(l+r)>>1;
     9         LL m3=mid*mid*mid;
    10         if(m3==a*b&&a%mid==0&&b%mid==0) return 1;
    11         else if (m3>a*b) r=mid-1;
    12         else l=mid+1;
    13     }
    14     if(l==r&&l*l*l==a*b&&a%l==0&&b%l==0) return 1;
    15     return 0;
    16 }
    17 int main()
    18 {
    19     int n;
    20     LL a,b;
    21     scanf("%d",&n);
    22     while(n--){
    23         scanf("%lld%lld",&a,&b);
    24         if(solve(a,b)) puts("Yes");
    25         else puts("No");
    26     }
    27     return 0;
    28 }
  • 相关阅读:
    开发导致的内存泄露问题,运维小伙伴儿这样排查不背锅
    JVM垃圾回收器、内存分配与回收策略
    笔试编程(二) | 7种常见的排序算法解析(附实现代码)
    HBase高级特性、rowkey设计以及热点问题处理
    Geotools创建Feature的两种方式
    geotools实现追加数据到数据库
    Java连接mysql数据库经典代码
    leaflet加载高德地图和Geoserver的WMS服务
    geotools学习之连接数据库并获取数据
    LeaFlet中切片图层使用自定义坐标系
  • 原文地址:https://www.cnblogs.com/zzqc/p/7262283.html
Copyright © 2011-2022 走看看