• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
magicat
博客园    首页    新随笔    联系   管理    订阅  订阅
B. Plus and Multiply

B. Plus and Multiply

手模拟了一下

image
观察每个式子,化简得到:

$a^x + by = n $
由于$a^x$是指数增长,直接枚举它的幂次即可,注意对$a=1$的情况特判
//  AC one more times

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define inf64 0x3f3f3f3f3f3f3f3f

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long,long long> pll;

const int mod = 1e9 + 7;
const int N = 1e5 + 10;

void solve()
{
    ll n, a, b;
    cin>>n>>a>>b;
    if(b == 1)
    {
        cout<<"YES"<<endl;
        return;
    }
    if(a == 1)
    {
        if((n - 1) % b == 0)
        {
            cout<<"YES"<<endl;

        }
        else
            cout<<"NO"<<endl;
        return;
    }
    bool ok = false;
    for(ll i = 0; pow(a, i) <= n; i++)
    {
        ll t1 = pow(a, i);
        if((n - t1) % b == 0)
            ok = true;
    }
    if(ok)
        cout<<"YES"<<endl;
    else cout<<"NO"<<endl;


    return;
}


int main()
{
    //std::ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    // 特殊输入 请注释上方,如__int128等
    int TC = 1;

    cin >> TC;
    for(int tc = 1; tc <= TC; tc++)
    {
        //cout << "Case #" << tc << ": ";
        solve();
    }



    return 0;
}

本文来自博客园,作者:magicat,转载请注明原文链接:https://www.cnblogs.com/magicat/p/17301315.html

posted on 2023-04-09 22:29  magicat  阅读(41)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3