zoukankan      html  css  js  c++  java
  • Codeforces Round #426 (Div. 2) C. The Meaningless Game (二分 or pow函数)

     

    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的数,如果你让其中一个数乘以x,则另一个数要乘以x的平方,问这样的操作若干次能否得到a和b?

    解题思路:刚开始就只是想到了使用搜索的方法进行暴力求解,但是看了一下数据发现这样是绝对会超时的就果断放弃了。没想法看了别人的博客发现,a*b一定是一个数的三次方并且a和b取余这个数都是为0的,只要找到这个数就能进行判断了。

    有两种做法:

    1.二分:

     1 #include <iostream>
     2 #include <bits/stdc++.h>
     3 #define LL long long
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     LL a,b;
     9     int t;
    10     while(~scanf("%d",&t))
    11     {
    12         while(t--)
    13         {
    14             scanf("%I64d%I64d",&a,&b);
    15             LL l=1,r=1000005,mid;
    16             int flag=0;
    17             while(l<=r)
    18             {
    19                 mid=(l+r)/2;
    20                 //printf("%lld
    ",mid);
    21                 if(mid*mid*mid==a*b&&a%mid==0&&b%mid==0)
    22                 {
    23                     printf("Yes
    ");
    24                     flag=1;
    25                     break;
    26                 }
    27                 else if(mid*mid*mid<a*b)
    28                 {
    29                     l=mid+1;
    30                 }
    31                 else
    32                 {
    33                     r=mid-1;
    34                 }
    35             }
    36             if(!flag)
    37             {
    38                 printf("No
    ");
    39             }
    40         }
    41     }
    42     return 0;
    43 }
    View Code

    2.pow()+round()函数

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <bits/stdc++.h>
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     double a,b;
    11     int t;
    12     while(~scanf("%d",&t))
    13     {
    14         while(t--)
    15         {
    16           scanf("%lf%lf",&a,&b);
    17           long long x,y,z;
    18           double c=round(pow(a*b,1.0/3));
    19           x=a,y=b,z=c;
    20           if(x%z==0&&y%z==0&&z*z*z==x*y)
    21           printf("Yes
    ");
    22           else
    23           printf("No
    ");
    24         }
    25     }
    26     return 0;
    27 }
    View Code

    知识点:

    ceil(x)返回不小于x的最小整数值(然后转换为double型)。
    floor(x)返回不大于x的最大整数值。
    round(x)返回x的四舍五入整数值。

  • 相关阅读:
    Css预编语言以及区别
    CSS完成视差滚动效果
    理解ASP.NET Core验证模型 Claim, ClaimsIdentity, ClaimsPrincipal
    EF Expression 扩展
    总结 jion,group join 基于方法的查询与查询表达式 对比
    EF 查询表达式 join
    EF中 GroupJoin 与 Join
    css中“~”和“>”是什么意思
    jquery基于form-data文件上传
    针对 easyui boolean 布尔值 的处理
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/7267354.html
Copyright © 2011-2022 走看看