51nod 1829 函數

來源:互聯網
上載者:User

標籤:info   input   bit   tput   ++   pre   怎麼   http   覆蓋   

想知道f:A->B這個函數(其中|A|=n, |B|=m)的所有映射關係要使B的每個元素都要被A的一個元素覆蓋到。數字可能很大你只要輸出方案數模1,000,000,007即可。Input
一共一行兩個數,n和m。(1<=n,m<=1,000,000)
Output
一共一行包含一個方案數。
Input樣本
2 2
Output樣本
2

話說這個題當時並沒有想明白怎麼去做

給定n個有區別的小球,裝到m個有區別的箱子裡,且沒有箱子是空的,求方案數

所以就是C(m,0)*m^n-C(m,1)*(m-1)^n+...-...

#include<bits/stdc++.h>using namespace std;#include<bits/stdc++.h>using namespace std;const int MD=1e9+7,N=2e6+5;int f[N],v[N];int C(int n,int m){    if(m<0||m>n) return 0;    return 1LL*f[n]*v[m]%MD*v[n-m]%MD;}int la(int a,int x){    int ans=1;    for(;x;a=a*1LL*a%MD,x>>=1)if(x&1)ans=ans*1LL*a%MD;    return ans;}int main(){    f[0]=1;    for (int i=1; i<N; i++) f[i]=1LL*f[i-1]*i%MD;    v[N-1]=la(f[N-1],MD-2);    for(int i=N-2; i>=0; i--) v[i]=v[i+1]*(i+1LL)%MD;    int ans=0,n,m,t=-1;    cin>>n>>m;    for(int i=0;i<=m;i++)t=-t,ans=(t*C(m,i)*1LL*la(m-i,n)%MD+ans)%MD;    cout<<(ans+MD)%MD;    return 0;}

 

 

 

51nod 1829 函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.