网易2017春招笔试真题编程题集合(12)——分饼干

参考:http://blog.csdn.net/wwe4023/article/details/70171648的内容

//

import java.util.*;
public class Main {
    public static void main(String[] args) {    
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        int n = Integer.parseInt(in.nextLine());
        System.out.println(combinationCount(line,n));
    }

    public static long combinationCount(String s,int n){
        int len = s.length();
        long[][] dp = new long[len+1][];
        for(int i = 0; i <= len; i++){
            dp[i] = new long[n];
        }
        dp[0][0] = 1;

        for(int i = 1; i <= len; i++){
            for(int j = 0; j < n; j++){
                if(s.charAt(i-1) == 'X'){
                    for(int k = 0; k <= 9; k++){
                        int newJ = (j*10+k) % n;
                        dp[i][newJ] += dp[i-1][j]; 
                    }
                }
                else
                {
                    int newJ = (j*10+(s.charAt(i-1)-'0'))% n;
                    dp[i][newJ] += dp[i-1][j]; 
                }
            }
        }

       /* for(int i=0;i<len+1;i++)
        {
            for(int j=0;j<n;j++)
                System.out.print(dp[i][j]+"   ");

           System.out.println();    
            }
        */
        return dp[len][0];


    }
}

 

posted @ 2017-06-08 11:37  CYTing  阅读(656)  评论(0编辑  收藏  举报