【Codeforces 490C】Hacking Cypher

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

让你把一个字符串分成左右两个部分 形成两个正数 使得这两个正数一个能被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());
        }
    }
}
posted @ 2019-03-04 13:57  AWCXV  阅读(114)  评论(0编辑  收藏  举报