题解
2026-01-12 10:28:44
发布于:江苏
10阅读
0回复
0点赞
题意:
本题考研我们对数字遍历求数位的能力和mod的用法熟练。本质也是一个简单模拟(小妙招,为了防止溢出,建议各位每次操作之后立刻取余)
操作:见注释
#include <iostream>
using namespace std;
const int MOD = 1e9 + 7;
// 检查数字x的数位是否包含a
bool hasDigit(int x, int a) {
while (x > 0) {
if (x % 10 == a) return true;
x /= 10;
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, a;
cin >> n >> a;
long long res = 1; // 用long long防止溢出
for (int i = 1; i <= n; ++i) {
// 跳过:能被a整除 或 数位包含a
if (i % a == 0 || hasDigit(i, a)) continue;
res = (res * i) % MOD;
}
cout << res << endl;
return 0;
}
这里空空如也


有帮助,赞一个