HDU-1465- difficult one series (inclusion and exclusion)

link:

https://vjudge.net/problem/HDU-1465

Meaning of the questions:

We often feeling, to do one thing really is not easy, indeed, the failure easier than success!
Success of the "one" thing yet it is not easy, if you want to always be successful and always never fail, it is more difficult, as always, like to spend money to make money easier than the truth.
Having said that, I still have to tell you, to fail to a certain extent it is not easy. For example, when I was in high school, there is a magical girl, the English exam, even the 40 multiple-choice all wrong! We all learned probability theory, the probability of such a situation should arise know, so far I think this is a wonderful thing. If you apply a classic comment, we can summarize: a people a wrong choice is not difficult, the difficulty is all wrong, a wrong.

Unfortunately, such a small probability event took place, and all around us:
the way it is --HDU have a network name called the male students in 8006, to make countless friends, the student playing the romance recently, while giving n number of users each person wrote a letter, which are nothing, worse, that he should put all the letters are installed wrong envelopes! Attention, all is installed wrong yo!

The question now is: Please help 8006 poor students to calculate, how many the wrong way on a possible total of it?

Ideas:

Consider all installation method has n! , Minus the equipment installed on the method.
There is mounted on the i-th C (n, i) * ( ni) !. You can optimize the formula, n! - n / 1 + n / 2 ...!!!!

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e5+10;
const int MOD = 1e9+7;

LL F[30];

void Init()
{
    F[0] = F[1] = 1;
    for (int i = 1;i <= 20;i++)
        F[i] = F[i-1]*i;
}

int main()
{
    Init();
    int n;
    while(~scanf("%d", &n))
    {
        LL res = F[n];
        LL tag = -1;
        for (int i = 1;i <= n;i++, tag = -tag)
            res = res+tag*F[n]/F[i];
        printf("%lld\n", res);
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11809018.html