C. Hacking Cypher

来源:互联网 发布:山东理工大学知乎 编辑:程序博客网 时间:2024/05/18 03:08


http://codeforces.com/contest/490/problem/C

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int MAX = 1000010;char str[MAX];int a,b;int ma[MAX],mb[MAX];long long Pow(long long x, int y, int mod){///快速幂    long long res = 1;    while(y >= 1){        if(y & 1)   res = res * x, res %= mod;        y >>= 1;        x = ((x % mod)*(x % mod)) % mod;    }    return res;}int main(){    scanf("%s",str);    scanf("%d%d",&a,&b);    int len = strlen(str);    memset(ma, 0, sizeof(ma));    memset(mb, 0, sizeof(mb));    ma[0] = (str[0] - '0') % a;    for(int i=1; i<len; i++)        ma[i] = (ma[i-1] * 10 + str[i] - '0') % a;    mb[len - 1] = (str[len-1] - '0') % b;    for(int i=len-2; i>=0; i--){        int tmp = len - 1 - i;        mb[i] = ((str[i] - '0') * Pow(10, tmp, b) + mb[i+1]) % b;    }///预先记录取模之后的值 不要每次都计算 不然会TLE    bool flag = false;    for(int i=0; i<=len-2; i++){///暴力枚举切开的位置        if(str[i+1] == '0') continue;        if(!ma[i] && !mb[i+1]){            flag = true;            puts("YES");            for(int j=0; j<=i; j++)                printf("%c",str[j]);            printf("\n");            for(int j=i+1; j<len; j++)                printf("%c",str[j]);            printf("\n");            break;        }    }    if(!flag)   puts("NO");    //system("pause");    return 0;}

0 0
原创粉丝点击