Codeforces 490C Hacking Cypher【前缀模+后缀模+暴力】

C. Hacking Cypher

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarpus participates in a competition for hacking into a new secure messenger. He‘s almost won.

Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits!

Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by a as a separate number, and the second (right) part is divisible by b as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values a and b.

Help Polycarpus and find any suitable method to cut the public key.

Input

The first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to 106 digits. The second line contains a pair of space-separated positive integers a, b (1?≤?a,?b?≤?108).

Output

In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines — the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by a, and the right part must be divisible by b. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them.

If there is no answer, print in a single line "NO" (without the quotes).

Examples

Input

116401024
97 1024

Output

YES
11640
1024

Input

284254589153928171911281811000
1009 1000

Output

YES
2842545891539
28171911281811000

Input

120
12 1

Output

NO

题目大意:

给你一个长度不超过1000000的一个整数,让你将其分成两个整数(当然不包含前导0咯),使得第一个数是a的倍数,第二个数是b的倍数。

思路:

1、暴力维护一个前缀模,再暴力维护一个后缀模。

2、然后枚举每一个点,如果其前缀模和后缀模都是0,而且第二个数不包含前导0的情况出现,那么就输出YES。否则输出NO

---------------------

#include<stdio.h>
#include<string.h>
using namespace std;
char a[1000050];
int bb[1000050];
int cc[1000050];
int main()
{
    while(~scanf("%s",a))
    {
        int b,c;
        scanf("%d%d",&b,&c);
        int n=strlen(a);
        bb[0]=(a[0]-‘0‘)%b;
        for(int i=1;i<n;i++)
        {
            bb[i]=(bb[i-1]*10+a[i]-‘0‘)%b;
        }
        cc[n-1]=(a[n-1]-‘0‘)%c;
        int tmp=1;
        for(int i=n-2;i>=0;i--)
        {
            tmp=tmp*10%c;
            cc[i]=(cc[i+1]+tmp*(a[i]-‘0‘))%c;
        }
        int flag=0;
        for(int i=0;i<n;i++)
        {
            if(bb[i]==0&&i+1<n&&a[i+1]!=‘0‘&&cc[i+1]==0)
            {
                flag=1;
                printf("YES\n");
                for(int j=0;j<=i;j++)
                {
                    printf("%c",a[j]);
                }
                printf("\n");
                for(int j=i+1;j<n;j++)
                {
                    printf("%c",a[j]);
                }
                printf("\n");
                break;
            }
        }
        if(flag==0)printf("NO\n");
    }
}

  

原文地址:https://www.cnblogs.com/passion-sky/p/9931291.html

时间: 2024-08-19 02:54:46

Codeforces 490C Hacking Cypher【前缀模+后缀模+暴力】的相关文章

CodeForces 490C Hacking Cypher

Hacking Cypher Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 490C Description Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won. Having

codeforces 490C. Hacking Cypher 解题报告

题目链接:http://codeforces.com/problemset/problem/490/C 题目意思:给出一个可能有10^6 位长的字符串且没有前导0的整数,问能否一分为二,使得前面的一部分被 a 整除 且 后面那部分被 b 整除,可以的话输出 “YES” 和 划分后的两部分,否则输出“NO”. 看到字符串这么长,一下子就吓怕了---用 mod 即可!这些要靠数学积累吧...... 好常规的方法做——暴力枚举,当然不能遗漏啦,所以从左至右直到倒数第二个数字,都要算出mod a 的结果

整除k的最大连续子区间(前缀和取模)(2017美团笔试)

题意:一个数n,给出n个数,再给一个数k.求能整除k的连续区间和所在区间的最大长度.bc85场1001的升级版. 题解:刚拿到题的时候没看清是连续区间,就瞎想dp.发现连续区间后,想尺取法,发现这道题是离散的,没法尺取,也没法二分. 正解应该是前缀和取模.若(sum[j]-sum[i])%k==0则区间[i,j]的和是能整除k的.注意,前缀和的第一项是0. 预处理完成后,考虑如何求最大区间长.从前往后扫一次,记录每个值最早出现的位置.从后往前扫一次,以便记录每个值最后出现的位置.当值为0时,则记

codeforces 432D D . Prefixes and Suffixes(后缀数组)

题目链接: codeforces 432D 题目大意: 给出一个字符串,求有多少种长度的前缀和后缀相等,并且得到的这个子串在原字符串中出现的次数. 题目分析: 首先利用后缀数组处理出sa[i]代表排名第i位的后缀的起始位置 处理出rank[i]代表第i个位置起始的后缀的rank 处理出height[i]代表排名第i位的和排名i-1位的公共前缀的长度. 那么我们要找后缀和前缀相等的就是找到rank[0],然后按照排名,向前向后遍历,任意两个后缀的公共前缀就是他们[i,j]区间内所有height的最

自增、自减运算符的前缀和后缀

试卷中有这么一道题目: 1 2 int a = 4; (++a) += i; 求a的数值,正确答案是10. 如果你认为这道题重点只是考察运算符优先级,可能很容易得到正确的答案. 但是,考虑过为什么下面的代码无法编译么? 自己在笔试时,考虑到了关于表达式作为赋值运算符左值的问题,但是自己确实又对重载"++"操作符的实现机制和函数原型不很了解,就误认为"a++"和"++a"这两种写法都不能作为赋值运算符左值,从而以为这道题出错了,或者故意考察这一点,

c++前缀和后缀++

1,c++规定后缀形式的++操作符有一个int行的参数,被调用时,编译器自动加一个0作为参数给他 2,前缀返回一个reference,后缀返回一个const对象 /////////////////////////////////////////////////////////////////////////////// // // FileName : meffect_item5.h // Version : 0.10 // Author : Ryan Han // Date : 2013/11

递归算法(二)&mdash;&mdash;前缀转后缀

源码:pretopost.cpp #include "stdafx.h" #include <stdio.h> #include <stack> /************************************************************************/ /* 前缀转后缀 */ /************************************************************************

字符串前缀,真前缀,后缀,真后缀,及前缀函数

举个例子,如字符串 ababc 首先,不考虑空字符,所有的前缀有a, ab, aba, abab, ababc,其中真前缀有a, ab, aba, abab 同理可以理解后缀,真前(后)缀就是指不包含自身的前(后)缀 前缀函数next[j]是指某个字符串的最长真后缀同时也是它的前缀的子串长度.不太理解可以看下面的例子 a -> 0 ab -> 0 aba -> 1 abab -> 2 ababc -> 0 前缀函数在字符串的匹配中用的较多,如KMP等.它主要是表明在一次匹配失

中缀、前缀、后缀表达式的转换

中缀表达式:a+b*c-(d+e) 第一步:按照运算符的优先级对所有的运算单位加括号:式子变成了:((a+(b*c))-(d+e)) 第二步:转换前缀与后缀表达式 前缀(波兰式):把运算符号移动到对应的括号前面 则变成了:-( +(a *(bc)) +(de)) 把括号去掉:-+a*bc+de 前缀式子出现 后缀(逆波兰式):把运算符号移动到对应的括号后面 则变成了:((a(bc)* )+ (de)+ )- 把括号去掉:abc*+de+- 后缀式子出现