CF 294C(Java大数做计数)

题目链接:http://codeforces.com/contest/294/problem/C

代码:

import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        BigInteger[] fir;
        fir = new BigInteger[1050];
        fir[0] = fir[0].ONE;
        for(int i=1; i<=1000; i++)
            fir[i] = fir[i-1].multiply(BigInteger.valueOf(i));
        BigInteger[] sec;
        sec = new BigInteger[1050];
        sec[0] = sec[0].ONE;
        for(int i=1; i<=1000; i++)
            sec[i] = sec[i-1].multiply(BigInteger.valueOf(2));
        
        int n,m;
        boolean flag[];
        flag = new boolean[1050];
        for(int i=0; i<=1000; i++) 
            flag[i] = false;
          
        n = cin.nextInt();
        m = cin.nextInt();   
        
        int last = 0;
        int sta[];
        sta = new int[1050];
        int cnt = 0;
        
        for(int i=1; i<=m; i++)
        {
            int temp;
            temp = cin.nextInt();
            flag[temp] = true;
        }
        
        for(int i=1; i<=n; i++)
        {
            if(flag[i])
            {    
                if(i-1-last > 0)
                {
                    sta[cnt++] = i - 1 - last;
                }
                last = i;
            }
            if(i == n && flag[i] == false)
            {
                if(i-last > 0)
                {
                    sta[cnt++] = i- last;
                }
            }
        }
        BigInteger ans;
        ans = fir[n-m]; 

        for(int i=0; i<cnt; i++)
        {
            int k = sta[i];
            ans = ans.divide(fir[k]);
            if((i == 0 && !flag[1])||(i == cnt-1 && !flag[n]))
                continue;
            
            ans = ans.multiply(sec[k-1]); 
        }
        
        ans = ans.mod(BigInteger.valueOf(1000000007));
        System.out.println(ans);
    }
}
View Code
原文地址:https://www.cnblogs.com/acmdeweilai/p/3402222.html