zoukankan      html  css  js  c++  java
  • C. New Year and Permutation

    Recall that the permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

    A sequence aa is a subsegment of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l,r][l,r], where l,rl,r are two integers with 1lrn1≤l≤r≤n. This indicates the subsegment where l1l−1 elements from the beginning and nrn−r elements from the end are deleted from the sequence.

    For a permutation p1,p2,,pnp1,p2,…,pn, we define a framed segment as a subsegment [l,r][l,r] where max{pl,pl+1,,pr}min{pl,pl+1,,pr}=rlmax{pl,pl+1,…,pr}−min{pl,pl+1,…,pr}=r−l. For example, for the permutation (6,7,1,8,5,3,2,4)(6,7,1,8,5,3,2,4) some of its framed segments are: [1,2],[5,8],[6,7],[3,3],[8,8][1,2],[5,8],[6,7],[3,3],[8,8]. In particular, a subsegment [i,i][i,i] is always a framed segments for any ii between 11 and nn, inclusive.

    We define the happiness of a permutation pp as the number of pairs (l,r)(l,r) such that 1lrn1≤l≤r≤n, and [l,r][l,r] is a framed segment. For example, the permutation [3,1,2][3,1,2] has happiness 55: all segments except [1,2][1,2] are framed segments.

    Given integers nn and mm, Jongwon wants to compute the sum of happiness for all permutations of length nn, modulo the prime number mm. Note that there exist n!n! (factorial of nn) different permutations of length nn.

    Input

    The only line contains two integers nn and mm (1n2500001≤n≤250000, 108m109108≤m≤109, mm is prime).

    Output

    Print rr (0r<m0≤r<m), the sum of happiness for all permutations of length nn, modulo a prime number mm.

    Examples
    input
    Copy
    1 993244853
    
    output
    Copy
    1
    
    input
    Copy
    2 993244853
    
    output
    Copy
    6
    
    input
    Copy
    3 993244853
    
    output
    Copy
    32
    
    input
    Copy
    2019 993244853
    
    output
    Copy
    923958830
    
    input
    Copy
    2020 437122297
    
    output
    Copy
    265955509
    
    Note

    For sample input n=3n=3, let's consider all permutations of length 33:

    • [1,2,3][1,2,3], all subsegments are framed segment. Happiness is 66.
    • [1,3,2][1,3,2], all subsegments except [1,2][1,2] are framed segment. Happiness is 55.
    • [2,1,3][2,1,3], all subsegments except [2,3][2,3] are framed segment. Happiness is 55.
    • [2,3,1][2,3,1], all subsegments except [2,3][2,3] are framed segment. Happiness is 55.
    • [3,1,2][3,1,2], all subsegments except [1,2][1,2] are framed segment. Happiness is 55.
    • [3,2,1][3,2,1], all subsegments are framed segment. Happiness is 66.

    Thus, the sum of happiness is 6+5+5+5+5+6=32

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
    int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1000000007;
    const int N = 1005;
    //if(x<0 || x>=r || y<0 || y>=c)
     
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
     
    int main()
    {
        ll res=0,n, m;
        cin >> n >> m;
        vector<ll> f(n+2,1);
        rep(i, 1, n+1)
        {
            f[i] = f[i - 1]*i%m;
        }
        rep(len, 1, n)
        {
            res += (f[len] * f[n - len+1]%m) * (n - len + 1) % m;
            res %= m;
        }
        cout << res << endl;
        return 0;
    }
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
    int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1000000007;
    const int N = 1005;
    //if(x<0 || x>=r || y<0 || y>=c)
     
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
     
    int main()
    {
        ll res=0,n, m;
        cin >> n >> m;
        vector<ll> f(n+2,1);
        rep(i, 1, n+1)
        {
            f[i] = f[i - 1]*i%m;
        }
        rep(len, 1, n)
        {
            res += (f[len] * f[n - len+1]%m) * (n - len + 1) % m;
            res %= m;
        }
        cout << res << endl;
        return 0;
    }
  • 相关阅读:
    -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
    map合并,相同键对应的值相加
    oracle截取字符串去掉字段末尾指定长度的字符
    springMVC结合AjaxForm上传文件
    hibernate中指定非外键进行关联
    Maven安装及MyEclipse中使用Maven
    js判断字符串出现的次数
    PL/SQL如何调试sql语句、存储过程
    如何让pl/sql developer记住密码,实现快速登录
    【学亮IT手记】jQuery each()函数用法实例
  • 原文地址:https://www.cnblogs.com/dealer/p/12824615.html
Copyright © 2011-2022 走看看