日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

srm#397_div1_500pt 矩阵乘法+快速模幂

發布時間:2024/4/17 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 srm#397_div1_500pt 矩阵乘法+快速模幂 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目地址:srm#397_div1_500

題目描述:

Problem Statement

?

NOTE: This problem statement contains superscripts that may not display properly if viewed outside of the applet.


You are given ints n and k. Return the value of the sum 1k + 2k + 3k + ... + nk modulo 1000000007.

Definition

?
Class:SumOfPowers
Method:value
Parameters:int, int
Returns:int
Method signature:int value(int n, int k)
(be sure your method is public)

Limits

?
Time limit (s):2.000
Memory limit (MB):64

Constraints

-n will be between 1 and 109, inclusive.
-k will be between 1 and 50, inclusive.

Examples

0)?
?
5
1
Returns: 15
Here, we have arithmethic progression: 1 + 2 + 3 + 4 + 5 = 15.
1)?
?
4
2
Returns: 30
Just a little bit more complicated example here: 12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30.
2)?
?
13
5
Returns: 1002001
This one would be harder to check by hand.
3)?
?
123456789
1
Returns: 383478132

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. ? ??


構造一個k+2階矩陣;

這里n比較大 顯然我們接受不了O(n)的復雜度

最好的方法就是像求Fibonacci數列第n項一樣用矩陣乘法+快速冪做


要實現求和長度在k左右的遞推式

首選(n+1)^k-n^k=sigma(c[k][i]*n^i);

?

直接看代碼里面的矩陣構造吧


wa了幾次

1 組合數是要求到c[52][i]的

2 因為mod 10^9+7 所以存在的數都是可能接近int上限的 ?要進行+運算 所以要用long long 存儲


代碼:

#include<iostream>typedef long long inta ;using namespace std;struct Matrix {inta m[60][60];};inta n; // 用來表示維度inta c[60][60]; //組合數const inta mod=1000000007;void init() {for(inta i=0;i<=55;i++)c[i][0]=1;for(inta i=0;i<55;i++)for(inta j=0;j<=i;j++)c[i+1][j+1]=c[i][j+1]+c[i][j];}Matrix multi(Matrix a,Matrix b) {Matrix ans;for(inta i=0;i<n;i++)for(inta j=0;j<n;j++){inta c=0;for(inta k=0;k<n;k++)c=(c+a.m[i][k]*b.m[k][j])%mod;ans.m[i][j]=c;}return ans;}Matrix quick_mod(Matrix a,inta b) {Matrix ans;Matrix p=a;for(inta i=0;i<n;i++)for(inta j=0;j<n;j++)ans.m[i][j]=(i==j?1:0);while(b){if(b&1){ans=multi(ans, p);b--;}b>>=1;p=multi(p, p);}return ans;}class SumOfPowers {public :inta value(inta nn,inta k){init();n=k+2;Matrix A;for(inta i=0;i<n;i++) // so importantfor(inta j=0;j<n;j++)A.m[i][j]=0;for(inta i=0;i<k+1;i++)for(inta j=0;j<=i;j++)A.m[i][j]=c[i][j]%mod;for(inta i=0;i<k+1;i++)A.m[k+1][i]=c[k][i]%mod;A.m[k+1][k+1]=1;A=quick_mod(A, nn-1);inta ans=0;for(inta i=0;i<k+2;i++)ans=(ans+A.m[k+1][i])%mod;return ans;} };int main() {inta nn,k;cin>>nn>>k;SumOfPowers obj;cout<<obj.value(nn, k)<<endl;}
tc上提交沒有main()



轉載于:https://www.cnblogs.com/jingqi814/p/3644368.html

總結

以上是生活随笔為你收集整理的srm#397_div1_500pt 矩阵乘法+快速模幂的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。