hdu 4602 Partition 快速冪,構造遞推關係 (13多校#1,1003)

來源:互聯網
上載者:User

題目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4602

解題思路: 1設對n的分解中k出現的次數是 g(n,k) ,首先發現k從n往下舉例的時候,前幾項都是一樣的(待證明)

                      然後只需要知道對n的劃分中,一共有多少個數。 考慮將n分成k個數,有C(n-1,k-1)種分發,於是總個數就是sigma k*C(n-1,k-1) ;

                     2 利用kC(n,k)=n C(n-1,k-1)  很容易求和  ,找到了a(n)=sigma...- S(n-1) ;再列舉一項相減就可以了。  最後得到a(n)=(2^(n-3)     ) *     (n+2),n>=3;

                     3 輸入n,k  將n-k+1帶入計算就可以了  ,注意特判n-k<0,==1,   幾種情況。

                     4n,k比較大, 記得使用快速冪,還有使用long long儲存結果;

#include<iostream>using namespace std;typedef long long inta;inta quick_mod(inta a,int b,int m){   inta ans=1;   a=a%m;   while(b>0)  {     if(b&1)     {        ans=(ans*a)%m;     }     b>>=1;     a=(a*a)%m;  }  return ans;}int main(){   int size;   cin>>size;   int n,k;   while(cin>>n>>k)   {      int temp=n-k;        if(temp<0)  cout<<0<<endl;      else if(temp==0)  cout<<1<<endl;      else if(temp==1)      {        cout<<2<<endl;      }//      else if(temp==2)//      {//         cout<<5<<endl;//      }      else      {          inta ans=quick_mod(2,temp-2,1000000007);          ans=(ans*(temp+3))%1000000007;          cout<<ans<<endl;      }   }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.