3157: 国王奇遇记

来源:互联网 发布:互联网金融与数据挖掘 编辑:程序博客网 时间:2024/04/27 15:08

3157: 国王奇遇记

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 720  Solved: 380
[Submit][Status][Discuss]

Description

Input

共一行包括两个正整数N和M。

Output

 

共一行为所求表达式的值对10^9+7取模的值。

Sample Input



5 3

Sample Output


36363

HINT



1<=N<=10^9,1<=M<=200

Source

Katharon+#1

[Submit][Status][Discuss]



据说本题还有O(m)的算法,不过苟蒻还不会。。。先挖个坑吧

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<queue>#include<set>#include<map>#include<stack>#include<bitset>#include<ext/pb_ds/priority_queue.hpp>using namespace std; const int N = 255;typedef long long LL;const LL mo = 1000000007; int n,m,f[N],C[N][N]; inline int Mul(const LL &x,const LL &y) {return x * y % mo;}inline int Add(const int &x,const int &y) {return x + y < mo ? x + y : x + y - mo;}inline int Dec(const int &x,const int &y) {return x - y >= 0 ? x - y : x - y + mo;} int ksm(int x,int y){    int ret = 1;    for (; y; y >>= 1)    {        if (y & 1) ret = Mul(ret,x);        x = Mul(x,x);    }    return ret;} int main(){    #ifdef DMC        freopen("DMC.txt","r",stdin);    #endif         cin >> n >> m; C[0][0] = 1;    if (m == 1) {cout << (1LL * n * (n + 1) / 2 % mo) << endl; return 0;}    int A = ksm(m,n),B = ksm(Dec(1,m),mo - 2);    f[0] = Mul(Mul(m,Dec(1,A)),B);    for (int i = 1; i <= m; i++)    {        C[i][0] = 1;        for (int j = 1; j <= i; j++)            C[i][j] = Add(C[i - 1][j],C[i - 1][j - 1]);    }    A = ksm(m,n + 1); B = ksm(Dec(m,1),mo - 2);    for (int i = 1; i <= m; i++)    {        A = Mul(A,n); int tmp = 0;        for (int j = 0; j < i; j++)        {            int now = Mul(C[i][j],f[j]);            tmp = (i - j & 1) ? Dec(tmp,now) : Add(tmp,now);        }        f[i] = Add(A,tmp); f[i] = Mul(f[i],B);    }    cout << f[m] << endl;    return 0;}

0 0
原创粉丝点击