Plus and Multiply(1500)

2024-08-29 10:52
文章标签 plus 1500 multiply

本文主要是介绍Plus and Multiply(1500),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Plus and Multiply

题面翻译

有一个无穷大的正整数集合 S S S,该集合按下面所述方法生成:

  • 数字 1 1 1 在集合 S S S 中。

  • 若数字 x x x 在该集合中,那么数 x × a x \times a x×a 和数 x + b x+b x+b 均在集合 S S S 中。(其中 a a a b b b 为给定常数)

现在给出数 n , a , b n,a,b n,a,b,请判断 n n n 是否在集合 S S S 中(此处给出的 a a a b b b 就是上述集合生成方法中的 a a a b b b),若在请输出 Yes,否则输出 No。多组数据。

令数据组数为 t t t,那么有 1 ≤ t ≤ 1 0 5 1 \leq t \leq 10^5 1t105 1 ≤ n , a , b ≤ 1 0 9 1 \leq n,a,b \leq 10^9 1n,a,b109

题目描述

There is an infinite set generated as follows:

  • $ 1 $ is in this set.
  • If $ x $ is in this set, $ x \cdot a $ and $ x+b $ both are in this set.

For example, when $ a=3 $ and $ b=6 $ , the five smallest elements of the set are:

  • $ 1 $ ,
  • $ 3 $ ( $ 1 $ is in this set, so $ 1\cdot a=3 $ is in this set),
  • $ 7 $ ( $ 1 $ is in this set, so $ 1+b=7 $ is in this set),
  • $ 9 $ ( $ 3 $ is in this set, so $ 3\cdot a=9 $ is in this set),
  • $ 13 $ ( $ 7 $ is in this set, so $ 7+b=13 $ is in this set).

Given positive integers $ a $ , $ b $ , $ n $ , determine if $ n $ is in this set.

输入格式

The input consists of multiple test cases. The first line contains an integer $ t $ ( $ 1\leq t\leq 10^5 $ ) — the number of test cases. The description of the test cases follows.

The only line describing each test case contains three integers $ n $ , $ a $ , $ b $ ( $ 1\leq n,a,b\leq 10^9 $ ) separated by a single space.

输出格式

For each test case, print “Yes” if $ n $ is in this set, and “No” otherwise. You can print each letter in any case.

样例 #1

样例输入 #1

5
24 3 5
10 3 6
2345 1 4
19260817 394 485
19260817 233 264

样例输出 #1

Yes
No
Yes
No
Yes

提示

In the first test case, $ 24 $ is generated as follows:

  • $ 1 $ is in this set, so $ 3 $ and $ 6 $ are in this set;
  • $ 3 $ is in this set, so $ 9 $ and $ 8 $ are in this set;
  • $ 8 $ is in this set, so $ 24 $ and $ 13 $ are in this set.

Thus we can see $ 24 $ is in this set.

The five smallest elements of the set in the second test case is described in statements. We can see that $ 10 $ isn’t among them.

思路:因为只有乘法和加法假设数为x,则我们先乘以a然后在加上b,与我们先加上b在乘以a其实是等价的,因为变换次数是无限的,因此我们可以想到n = a的x次方 + b的y次方,因为指数型函数指数爆炸式增长,因此我们来枚举x,枚举过程中我们只需要判断n - a的x次方对b取模是不是0即可

AC代码:

#include<bits/stdc++.h>using namespace std;typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, double>PDD;
const int N=2e5 +10;
const int MOD = 1e9 + 7;
const int INF=0X3F3F33F;
const int dx[]={-1,0,1,0,-1,-1,+1,+1};
const int dy[]={0,1,0,-1,-1,+1,-1,+1}; //马
const int dxx[]={-1,2,1,1,-1,2,-2,-2};
const int dyy[]={2,1,-2,2,-2,-1,-1,1};    
const int M = 1e7 + 10;int t;
int main()
{cin >> t;while(t --){ll n, a, b;cin >> n >> a >> b;if(a == 1){if((n - 1) % b == 0) puts("Yes");else puts("No");}else {int f = 0;for(ll i = 1; i <= n; i *= a){if((n - i) % b == 0){f = 1;break;}}if(f) puts("Yes");else puts("No");}}return 0;
}

这篇关于Plus and Multiply(1500)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1117658

相关文章

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

mybatis-plus QueryWrapper中or,and的使用及说明

《mybatis-plusQueryWrapper中or,and的使用及说明》使用MyBatisPlusQueryWrapper时,因同时添加角色权限固定条件和多字段模糊查询导致数据异常展示,排查发... 目录QueryWrapper中or,and使用列表中还要同时模糊查询多个字段经过排查这就导致只要whe

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

Mybatis Plus JSqlParser解析sql语句及JSqlParser安装步骤

《MybatisPlusJSqlParser解析sql语句及JSqlParser安装步骤》JSqlParser是一个用于解析SQL语句的Java库,它可以将SQL语句解析为一个Java对象树,允许... 目录【一】jsqlParser 是什么【二】JSqlParser 的安装步骤【三】使用场景【1】sql语

将Mybatis升级为Mybatis-Plus的详细过程

《将Mybatis升级为Mybatis-Plus的详细过程》本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率,通过本文,开发者可实现... 目录说明流程增加依赖修改配置文件注释掉MyBATisConfig里面的Bean代码生成使用IDEA生

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Spring Boot结成MyBatis-Plus最全配置指南

《SpringBoot结成MyBatis-Plus最全配置指南》本文主要介绍了SpringBoot结成MyBatis-Plus最全配置指南,包括依赖引入、配置数据源、Mapper扫描、基本CRUD操... 目录前言详细操作一.创建项目并引入相关依赖二.配置数据源信息三.编写相关代码查zsRArly询数据库数