C. Hacking Cypher



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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值