码迷,mamicode.com
首页 > 其他好文 > 详细

【Codeforces 490C】Hacking Cypher

时间:2019-03-04 14:21:57      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:ret   buffere   col   red   ==   print   next   sum   exce   

【链接】 我是链接,点我呀:)
【题意】


让你把一个字符串分成左右两个部分
形成两个正数
使得这两个正数一个能被a整除,一个能被b整除
找到任意一个解就可以

【题解】


枚举分割的断点i
枚举的时候用同余率算出来s[1..i]和a以及b取余的结果
怎么得到s[i+1..len-1]呢?
只要用s[1..len]-s[1..b]就可以了
乘的时候可能会爆int,小心处理

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = (int)1e6;
    static class Task{
        
        String s;
        long a,b;
        long sumb = 0,prea = 0,preb = 0;
        long aftb[] = new long[N+10];
        
        public void solve(InputReader in,PrintWriter out) {
            s = in.next();
            a = in.nextInt();b = in.nextInt();
            
            aftb[(int)s.length()] = 1%b;
            for (int i = (int)s.length()-1;i>=0;i--) {
                aftb[i] = aftb[i+1]*10%b;
            }
            for (int i = 0;i < (int)s.length();i++) {
                sumb = sumb*10 + s.charAt(i)-'0';
                sumb = sumb%b;
            }
            for (int i = 0;i < (int)s.length()-1;i++) {
                prea = prea * 10 + s.charAt(i)-'0';
                prea = prea%a;
                preb = preb * 10 + s.charAt(i)-'0';
                preb = preb%b;
                long temp = (sumb-preb*aftb[i+1]%b)%b;
                if (temp<0) temp+=b;
                if (temp==0 && prea ==0) {
                    if (s.charAt(i+1)=='0') continue;
                    out.println("YES");
                    for (int j = 0;j <=i;j++) {
                        out.print(s.charAt(j));
                    }
                    out.println();
                    for (int j = i+1;j < (int)s.length();j++) {
                        out.print(s.charAt(j));
                    }
                    return;
                }
            }
            out.println("NO");
            
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
        
        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}

【Codeforces 490C】Hacking Cypher

标签:ret   buffere   col   red   ==   print   next   sum   exce   

原文地址:https://www.cnblogs.com/AWCXV/p/10470292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!