本文主要是介绍CodeForces 490C Hacking Cypher,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
一串数字 从某个地方分开成两个数字 要求前面的数字被A整除 后面的被B整除 求分开的两个数字
思路:
假设我们将原串S这样分成两个数字XY 则X%A==0 Y%B==0
那么我们可以处理从头到i这个位置%A的值为多少 这样很容易判断第一个条件
对于第二个条件我们可以这样理解 S % B == ( X % B * 10^|Y| % B ) + Y % B
如果Y%B==0 那么 S % B == X % B * 10^|Y| % B
所以我们可以处理从头到i这个位置%B的值为多少 和 10的某次幂%B的值为多少 判断i位置是否满足上述等式
注意:Y的第一个字符不能是‘0’
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
typedef long long LL;
#define N 1000010char s[N];
LL a, b;
int ans;
LL A[N], B[N], T[N] = { 1 };int main() {scanf("%s", s + 1);cin >> a >> b;int len = strlen(s + 1);for (int i = 1; i <= len; i++) {A[i] = (A[i - 1] * 10 + s[i] - '0') % a;B[i] = (B[i - 1] * 10 + s[i] - '0') % b;T[i] = T[i - 1] * 10 % b;}ans = 0;for (int i = 2; i <= len; i++) {if (s[i] != '0' && A[i - 1] == 0) {if (B[i - 1] * T[len - i + 1] % b == B[len]) {ans = i;break;}}}if (ans) {puts("YES");for (int i = 1; i <= len; i++) {if (i == ans)putchar('\n');putchar(s[i]);}} elseputs("NO");return 0;
}
这篇关于CodeForces 490C Hacking Cypher的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!