Codeforces

来源:互联网 发布:粒子群算法缺点 编辑:程序博客网 时间:2024/05/17 17:44

C. The Meaningless Game

题目链接

分类:数学

1.题目描述

  • n(1n350000)场游戏,对于每场游戏有若干个回合组成,两个人的初始分均为1,每个回合赢的人当前分数乘上k2,输的人当前分数乘上k(每一回合的k都是不同的)。给你两个数a,b(1a,b109)问你这两个数是否为他们最终的分数?

2.解题思路

  • 显然我们容易发现最终状态是a=k21k2, b=k1k22的形式,我们考虑把两者相乘就有ab=k31k32,要保证合法,也就是k1k2=ab3,即ab必须能被开三次方!
    • 第一种做法:我们去枚举或者二分i3或者来判断是否存在ab=i3x成立,存在即合法。
    • 第二种做法:直接暴力开三次根号,根据计算机舍入规则判断是否合法(不推荐)。

3.AC代码

  • 第一种做法:
ll t[maxn];void init() {    for(int i = 0; i <= 1000000; i++) t[i] = (ll)i * i * i;}int main() {    init();    int n, a, b;    scanf("%d", &n);    while(n--) {        scanf("%d%d", &a, &b);        ll c = (ll)a * b;        int pos = lower_bound(t, t + maxn, c) - t;        //printf("pos=%d\n",pos);        if((ll)pos * pos * pos == c && a % pos == 0 && b % pos == 0) printf("Yes\n");        else printf("No\n");    }    return 0;}
  • 第二种做法:
#include <bits/stdc++.h>using namespace std;#define lson root << 1#define rson root << 1 | 1#define lent (t[root].r - t[root].l + 1)#define lenl (t[lson].r - t[lson].l + 1)#define lenr (t[rson].r - t[rson].l + 1)#define eps 1e-6#define e exp(1.0)#define pi acos(-1.0)#define fi first#define se second#define pb push_back#define mp make_pair#define SZ(x) ((int)(x).size())#define All(x) (x).begin(),(x).end()#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define Close() ios::sync_with_stdio(0),cin.tie(0)#define INF 0x3f3f3f3f#define maxn 100010#define N 1111typedef vector<int> VI;typedef pair<int, int> PII;typedef long long ll;typedef unsigned long long ull;const int mod = 1e9 + 7;/* head */int main() {#ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);    freopen("out.txt", "w", stdout);    long _begin_time = clock();#endif    int t;    ll a, b;    scanf("%d", &t);    while(t--) {        scanf("%lld%lld", &a, &b);        ll temp = ceil(pow(a * b * 1.0, 1.0 / 3));        if(temp * temp * temp != a * b || a % temp || b % temp) {            puts("No");        } else {            puts("Yes");        }    }#ifndef ONLINE_JUDGE    long _end_time = clock();    printf("time = %ld ms.", _end_time - _begin_time);#endif    return 0;}
原创粉丝点击