2013年10月6日 星期日

UVa 12022 - Ordering T-shirts

Problem link

Keyword: math, pre-calculation

Algorithm:

Run through all relational operators and find out how many ways can be formed.
For N=4, we have 3 relational operators, and each of these operators can be "less" or "equal." For each consequent K "equals" we have to divide N! by (K+1)! and we can run through all situations and find out the answer.
Why divide by (K+1)!? Because for a=b=c, we have 2 equals and a, b, c, can only form one solution. At first, a, b, c have permutation up to 3! ways but only 1 now, so we divide it by (K+1)!.
*We have N! solutions if there's no equal operator in the statement.
Once we have the answer, we can just store them in an array at compilation time to achieve faster solution.

Code:
#include<cstdio>

int main()
{
    int ans[] = { 1, 1, 3, 13, 75, 541, 4683, 47293, 545835, 7087261, 102247563, 1622632573 }, t, n;
    scanf( "%d", &t );
    while( t-- )
        scanf( "%d", &n ), printf( "%d\n", ans[ n ] );
    return 0;
}

沒有留言:

張貼留言