Codeforces: (C - New Year and Permutation) permutations

Do this type must think more about
not falling into the "dead end"
Sometimes, some of the problems, positive thinking and reverse thinking a try

In solving the problem of time thinking about the old number with the total number of minus unsatisfied
in fact being offered to meet the immediate requirements of the number of simpler

Think about the opposite of nearly two hours did not launch
day is enough

The whole number of 1-n arrangement
of this question directly meet demand interval, i.e. the interval equal to the difference between poor
and very poor to meet interval equal to the difference, in number of this interval must be continuous way
since it is continuous, direct enumeration poor or very poor range

Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[300000],n,m;
void init()
{
    ll ans=1;
    for(int i=1;i<=250000;i++)
    {ans=ans*i%m;
    a[i]=ans;}
}
int main()
{
    cin>>n>>m;
    init();
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        ans=(ans%m+(i%m*a[n-i+1]%m*a[i]%m)%m)%m;
    }
    cout<<ans<<endl;
    return 0;
}

Published 36 original articles · won praise 4 · Views 1387

Guess you like

Origin blog.csdn.net/qq_43781431/article/details/104707268