Codeforces - 834C. The Meaningless Game - 数学

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 1111
typedef 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值