Inclusion and exclusion - miscalculated the number of rows program UVA 10497

The problem is that the wrong row of a special arrangement problem.

Model: The n elements sequentially marked 1,2,3 ....... n, each seeking the number of elements are not aligned in their position.

The use of inclusion and exclusion, we have two solutions:

1.

The overall arrangement of methods A (n, n), i.e., n !, provided A i represents the number i of the i-th arrayed in all positions, clearly A i = (. 1-n-) !.

Similarly available Ai∩Aj = (n-2) !, then each have n elements are not arranged in the original position! -C (n, 1) * (n-1)! + C (n, 2) * (n-2) -..... +! (- 1) ^ n * C (n, n) * 1 !.

I.e. n *! (A-1/1 +1/2 -1/3 +1/4 -..... + (-!!!! 1) ^ n * 1 / n!).

As long as we pre-n factorial can get an answer.

2.

DP with the idea to apply the principle of inclusion and exclusion.

1,2 ...... wrong program number provided rows is dp [n], when we take any one of a number i of misalignment has to make two situations:

①: the number i to be interchanged with one of the other n-1 number, then the remaining n-2 mismatches rows ..dp [n] = (n-1) * dp [n-2].

②: i alia divisor n-1 row error number, and then select from a number n-1 and i interchanged ..dp [n] = (n-1) * dp [n-1].

Obviously these two forms of error are independent and mutually exclusive row, using the addition theorem, then we have: dp [1] = 0, dp [2] = 1, dp [n] = (n-1) * (dp [n-1] + dp [n-2]).

--------

note:

When n is large, the number of rows of the wrong program will become quite large! Therefore, the general theme will be the wrong row with precision.

[Title] template UVA 10497

Children are always sweet but they can sometimes make you feel bitter. In this problem, you will see
how Tintin, a ve year's old boy, creates trouble for his parents. Tintin is a joyful boy and is always
busy in doing something. But what he does is not always pleasant for his parents. He likes most to play
with household things like his father's wristwatch or his mother's comb. After his playing he places it
in some other place. Tintin is very intelligent and a boy with a very sharp memory. To make things
worse for his parents, he never returns the things he has taken for playing to their original places.
Think about a morning when Tintin has managed to `steal' three household objects. Now, in how
many ways he can place those things such that nothing is placed in their original place. Tintin does not
like to give his parents that much trouble. So, he does not leave anything in a completely new place;
he merely permutes the objects.
Input
There will be several test cases. Each will have a positive integer less than or equal to 800 indicating
the number of things Tintin has taken for playing. Each integer will be in a line by itself. The input
is terminated by a `-1' (minus one) in a single line, which should not be processed.
Output
For each test case print an integer indicating in how many ways Tintin can rearrange the things he has
taken.
Sample Input
2
3
4
-1
Sample Output
1
2
9

Meaning of the questions:

Q n number of program number wrong row.

 

Staggered ideas above, AC code with the idea is 2.

AC code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll m=1e10;
struct Bigint{
    ll s[1010];
    ll l;
    Bigint()
    {
        l=0;
        memset(s,0,sizeof(s));
    }
    void operator *=(int x)
    {
        ll d=0;
        for(int i=0;i<=l;i++)
        {
            d+=s[i]*x;
            s[i]=d%m;
            d/=m;
        }
        while(d)
        {
            s[++l]=d%m;
            d/=m;
        }
    }
    void print()
    {
        printf("%llu",s[l]);
        for(int i=l-1;i>=0;i--)
        {
            printf("%010llu",s[i]);
        }
    }
    void set(ll x)
    {
        s[l]=x%m;
        x/=m;
        while(x)
        {
            ++l;
            s[l]=x%m;
            x/=m;
        }
    }
}dp[1010];
Bigint operator +(Bigint b,Bigint& a)
{
    ll d=0;
    b.l=max(b.l,a.l);
    for(int i=0;i<=b.l;i++)
    {
        b.s[i]+=(d+a.s[i]);
        d=b.s[i]/m;
        b.s[i]%=m;
    }
    if(d)
    {
        b.s[++b.l]=d;
    }
    return b;
}
int n;
int main()
{
    //freopen("input.txt","r",stdin);
    dp[1].set(0);
    dp[2].set(1);
    for(int i=3;i<=800;i++)
    {
        dp[i]=dp[i-1]+dp[i-2];
        dp[i]*=(i-1);
    }
    while(~scanf("%d",&n)&&n!=-1)
    {
        dp[n].print();
        printf("\n");
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/cautx/p/11423478.html