您的位置:首页 > 其它

求数组的所有排列组合数

2017-12-06 16:07 323 查看
Problem Description

How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, …., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.

Input

The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, …., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.

Output

For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.

Sample Input

3

1 2 3

Sample Output

7

代码:

static List<String> list = new ArrayList<String>();
/**
* 求a中count个数的组合
* @param a          存放组合的数
* @param restCount  当前还需多少个数构成一个组合
* @param count      在a中挑选出count个数
* @param b            存放当前组合
* @param pos        指向a中的元素,
*/
public static void combination(int []a,int restCount,int count,int[] b,int pos){
if(restCount <= 0 || count > a.length){
return;
}

for(int i = pos;i >= restCount - 1;i-- ){
StringBuilder sb = new StringBuilder();
b[restCount - 1] = a[i];
if(restCount - 1 == 0){
for(int j:b){
sb.append(j);
}
if (!list.contains(sb.toString())) {
list.add(sb.toString());
}
}
combination(a, restCount-1, count, b, i-1);
}
}
public static void main(String[] args) {
int n ;
Scanner in = new Scanner(System.in);
n = in.nextInt();
int[] s = new int
;
for (int j = 0; j < s.length; j++) {
s[j] = in.nextInt();
}
for (int i = 0; i < s.length; i++) {
int[] b = new int[i+1];
combination(s, i+1, i+1, b, s.length - 1);
}
System.out.println(list.size());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: