zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 32 Almost Identity Permutations CodeForces

    A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.

    Let's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that pi = i.

    Your task is to count the number of almost identity permutations for given numbers n and k.

    Input
    The first line contains two integers n and k (4 ≤ n ≤ 1000, 1 ≤ k ≤ 4).

    Output
    Print the number of almost identity permutations for given n and k.

    Examples
    Input
    4 1
    Output
    1
    Input
    4 2
    Output
    7
    Input
    5 3
    Output
    31
    Input
    5 4
    Output
    76

    题意:
    给你一个整数n和k,问有多少个n的全排列中 下标和数值相等的个数是大于等于n-k 的。
    思路:
    那么我们不妨从n-k到n枚举 下标和数值相等的个数i,对于每一个i,我们可以从n中选择i个数,让他们在1,2,3,4,,,n这个排列中位置不变,剩下的n-i个数,不在自己的位置上,那么问题就转化为 对于每一个i,求C(n,i)a(i),a(i)是i个数的全排列,每一个数都不在原来位置上的排列种类数。 而a[i] 是一个 递推数列 a[i]=ia[i-1 ]+-1^i ,我们知道a[0]=1,这样顺推就可以求出所有i对答案的贡献,累加起来就是答案值了。

    细节见代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define rt return
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    ll a[maxn];
    
    ll C(ll m,ll n)//m 中 选 n 个
    {
        long long ans=1;
        for(long long k=1; k<=n; k++)
        {
            ans=(ans*(m-n+k))/k;
        }
        return ans;
    }
    
    int main()
    {
        //freopen("D:\common_text\code_stream\in.txt","r",stdin);
    	//freopen("D:\common_text\code_stream\out.txt","w",stdout);
        a[0]=1ll;
        a[1]=0ll;
        a[2]=1ll;
        a[3]=2ll;
        a[4]=9ll;
        int base=-1;
        repd(i,5,1010)
        {
            a[i]=1ll*i*a[i-1]+base;
            base*=-1;
        }
        ll n,k;
        cin>>n>>k;
        ll ans=0ll;
        repd(i,n-k,n)
        {
            ans+=C(n,i)*a[n-i];
        }
        cout<<ans<<endl;
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    Python常用模块学习
    如何在cmd下切换不同版本的Python
    Python3
    Python第二模块(文件和函数)
    Hibernate教程一览
    struts2框架一览
    Java正式day_04——嵌套循环
    AJAX
    JS+JQUERY
    Mybatis注意问题
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/11097987.html
Copyright © 2011-2022 走看看