24h購物| | PChome| 登入
2012-04-01 10:01:59| 人氣916| 回應0 | 上一篇 | 下一篇

[UVA] 12022 - Ordering T-shirts

推薦 0 收藏 0 轉貼0 訂閱站台

 D - Ordering T-shirts 

Background

Working in a boutique folding and putting in order T-shirts according to their sizes seems very easy. But is it really so simple?

The Problem

Given n objects of different sizes, how many different arrangements can be done using relationships "<" and "="?

For instance, with 2 objects, A and B, we have 3 possible arrangements:

A=B     A<B     B<A

With 3 objects, A, B and C, you must conclude that 13 different arrangements exist:

A=B=C     A=B<C     A<B=C     A<B<C     A<C<B     A=C<B     B<A=C     B<A<C     B<C<A     B=C<A     C<A=B     C<A<B     C<B<A

The Input

The first line of the input contains an integer, t, indicating the number of test cases. For each test case, one line appears, that contains a number n, 1<=n<=11, representing the number of objects.

The Output

For each test case, the output should contain a single line with the number representing the different arrangements you can do with n objects.

Sample Input

4
1
2
3
4

Sample Output

1
3
13
75

做法 : 窮舉 "<" "=" 的排列
再利用數學公式 n! / a!/b! ... 去做建表

#include <stdio.h>
#include <stdlib.h>
int step[12] = {}, ans[12] = {0, 1};
void Backtracking(int now) {
if(now >= 12)
return;
if(now > 0) {
int i, f = 1, c = now;
int tmp = 2, count = 2;
for(i = 2; i <= c+1; i++)
f *= i;
for(i = 0; i < c-1; i++) {
if(step[i] == step[i+1] && step[i] == 2)
count++, tmp *= count;
else {
if(step[i] == 2)
f /= tmp, tmp = 2, count = 2;
}
}
if(step[i] == 2)
f /= tmp, tmp = 2, count = 2;
ans[c+1] += f;
}
step[now] = 0;
Backtracking(now+1);
step[now] = 2;
Backtracking(now+1);
}
int main() {
int t, n;
Backtracking(0);
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
printf("%d\n", ans[n]);
}
return 0;
}

台長: Morris
人氣(916) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 12004 - Bubble Sort
此分類上一篇:[UVA] 12036 - Stable Grid

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文