3157: 国王奇遇记

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

[ 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值