首页 > 其他分享 >CF1542B Plus and Multiply

CF1542B Plus and Multiply

时间:2022-10-14 02:11:05浏览次数:74  
标签:CF1542B ch int yb Plus Multiply

CF1542B Plus and Multiply - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

\(T = \{a^x +yb \text{ } \vert \text{ } x, y \in N\}\) 和 \(S\) 相等。

证明:

  • \(S \subseteq T\):显然有 \(1 \in T\)。然后有 \((a^x+yb) \times a = a^{x+1} + (ay)b\),\((a^x + yb) + b = a^{x+1} + (y+1)b\)。
  • \(T \subseteq S\):\(a^x + yb\) 可以由 \(1\) 乘 \(x\) 次 \(a\) 再加 \(y\) 次 \(b\) 得到。

因此 \(S = T\)。

那么问题可以转化为 \(n\) 是否可以表示为 \(a^x + yb\),其中 \(x, y \in N\)?

容易发现当 \(a \ne 1\) 时,\(x\) 的范围很小。枚举 \(x\) 即可。然后检验 \(y\) 是否为 \(n - a^x\) 的因数。

\(a = 1\) 时容易特判:只需要检验 \(n \bmod b = 1\) 或 \(b = 1\) 即可(注意 \(X \bmod 1 = 0\))。

单组数据 \(\mathcal{O}(\log n)\)。

/*
 * @Author: crab-in-the-northeast 
 * @Date: 2022-10-14 01:41:35 
 * @Last Modified by: crab-in-the-northeast
 * @Last Modified time: 2022-10-14 01:52:57
 */

#include <bits/stdc++.h>
#define int long long

inline int read() {
    int x = 0;
    bool flag = true;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-')
            flag = false;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    if(flag)
        return x;
    return ~(x - 1);
}

signed main() {
    int T = read();
    while (T--) {
        int n = read(), a = read(), b = read();
        if (a == 1) {
            if (b == 1 || n % b == 1)
                puts("Yes");
            else
                puts("No");
            continue;
        }

        bool fl = false;
        for (int X = 1; X <= n; X *= a) {
            if ((n - X) % b == 0) {
                fl = true;
                break;
            }
        }

        puts(fl ? "Yes" : "No");
    }
    return 0;
}

标签:CF1542B,ch,int,yb,Plus,Multiply
From: https://www.cnblogs.com/crab-in-the-northeast/p/cf1542b.html

相关文章

  • Mybatis-plus分页查询和条件查询通用写法- 动态接口参数
    一查询条件VO/***@className:TeacherQueryVo*@description:讲师查询条件封装*@date:2020/11/18*@author:cakin*/@ApiModel("Teacher查询对象")@Datapubl......
  • mybatis-plus批量插入性能提升
    最近在引入mybatis-plus后发现其批量插入的性能不高,所以与mybatis的<foreach/>标签生成的sql插入性能做了对比测试环境:6核12线程,16g内存本地数据库,没有网络传输数据库......
  • 使用 ADManager Plus管理Microsoft Office 365
    Microsoft365浪潮席卷了全球各种规模和职能的组织。它使进入云变得容易且具有成本效益,而无需放弃熟悉的Microsoft服务器和客户端应用程序。其成功和在全球范围内广泛采......
  • ElementPlus的MessageBox自动关闭
    <template><el-rowclass="mb-4"><el-button@click="open"type="primary"show-confirm-button="false">Click</el-button></el-row></template><scripts......
  • SpringBoot+MyBatis Plus对Map中Date格式转换的处理
    在SpringBoot项目中,如何统一JSON格式化中的日期格式问题现在的关系型数据库例如PostgreSQL/MySQL,都已经对JSON类型提供相当丰富的功能,项目中对于不需要检索但是......
  • SQL Plus的一些限制
    写了一个SQL脚本,在SQL*Plus中执行的时候,居然遇到下面错误:stringbeginning""<fontsiz..."istoolong.maximumsizeis240characters.出现这个错误的原因:在SQLPlu......
  • MyBatisPlus笔记
    MyBatisPlus快速入门1.创建数据库mybatisplus2.创建user表并插入数据DROPTABLEIFEXISTSuser;CREATETABLEuser(idBIGINT(20)NOTNULLCOMMENT'主键I......
  • 前端_vue3引入Element-plus
    element-plus官网:https://element-plus.gitee.io/zh-CN/component/button.html1、安装element-plusnpminstallelement-plus-D2、在index.js中导入element-plusim......
  • MyBatis-Plus个人笔记
    第一章MyBatis-Plus入门1)MP简介官方地址:http://mp.baomidou.com代码发布地址:Github:https://github.com/baomidou/mybatis-plusGitee:https://gitee.com/baomi......
  • Mybatis-Plus实现分页
    1、导入maven依赖springboot版本2.7.3<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</art......