【数论】codeforces 327C Magic five

主要是学习如何写乘法逆,采用了分治法的基本策略。

import java.util.Scanner;


public class cf_327C {

	public static final long MOD = 1000000007;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		String str = cin.nextLine();
		long k = cin.nextLong();
		long t = str.length();
		long res = (modpow(2, (t*k))+MOD-1)%MOD;
		long den = (modpow(2, t)+MOD-1)%MOD;
		res = (res*inv(den))%MOD;
		
		long tmp = 0;
		for(int i=0; i<t; i++){
			if(str.charAt(i)=='0' || str.charAt(i)=='5'){
				tmp = (tmp+modpow(2, i))%MOD;
			}// end if
		}// end for loop
		res = (res*tmp)%MOD;
		
		System.out.println(res);
	}
	
	/**
	 * 
	 * @param number
	 * @param p
	 * @return modulo power
	 */
	public static long modpow(long number, long p){
		if(p == 0)
			return 1;
//		p is an odd number
		if(p%2 == 1)
			return (number*modpow((number*number)%MOD, p>>1)) % MOD;
//		p is an even number
		else
			return modpow((number*number)%MOD, p>>1) % MOD;
	}// end method
	
	public static long inv(long number){
		return modpow(number, MOD-2)%MOD;
	}// end method

}// end class


你可能感兴趣的:(【数论】codeforces 327C Magic five)