cf 729 B
[link]<Problem - B - Codeforces>
题意
给n, a, b, 问能否把1通过若干次*a或加b相互组合,得到n。
题解
首先如果一个a也不操作, 一定是 1 + nb的形式
如果选了了一个a,那么就是 a^k + pba^m + … + qb 这样的多项式形式,即 通过去减某个 a ^ x 把最高次幂去除后,每一项都应该带一个b所以 ( n - a ^ x) % b == 0 即成立
代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <unordered_map>
#include <cmath>
#include <stack>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
const int N = 2e5 + 10, M = 1100, INF = 0x3f3f3f3f, P = 131;
const LL mod = 1e9 + 7;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m;
int main (){
int t;
cin >> t;
while (t --) {
int n, a, b;
cin >> n >> a >> b;
bool ok = false;
for (LL x = 1; x <= n; x *= a) {
if ((n - x) % b == 0) ok = true;
if (a == 1|| ok) break;
}
if (ok) puts("Yes");
else puts("No");
}
return 0;
}
这篇博客探讨了一道来自Codeforces的编程题目,涉及动态规划和数学概念。文章解释了如何通过检查1到n是否可以通过乘以a或加上b的组合来达到,来判断给定的整数n是否可以被构造出来。主要思路是检查是否存在某个x使得(n-x)%b==0,从而确定解的存在性。
1万+





