题目地址: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 矩阵乘法+快速模幂相关推荐

  1. 求解斐波那契第n项的几种解法(含矩阵乘法+快速幂) Python实现

    斐波那契数列 首先我们来定义一下斐波那契数列: f(n)={0n = 01n = 1f(n−1)+f(n−2)n > 1f(n)= \begin{cases} 0 & \text {n ...

  2. 51Nod-1046 A^B Mod C【快速模幂】

    1046 A^B Mod C 基准时间限制:1秒 空间限制:131072KB 分值:0难度:基础题 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. I ...

  3. HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】

    问题链接:HDU1163 Eddy's digital Roots. 问题简述:参见上述链接. 问题分析:计算n^n的数根,一要快,二要简单.使用快速模幂计算,加上数论中的九余数定理就完美了. 程序说 ...

  4. CodeForces - 1514B AND 0, Sum Big【快速模幂】

    B. AND 0, Sum Big time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

  5. UVA11029 Leading and Trailing【快速模幂+数学】

    Apart from the novice programmers, all others know that you can't exactly represent numbers raised t ...

  6. HDU1420 Prepared for New Acmer【快速模幂】

    Prepared for New Acmer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  7. UVA11582 Colossal Fibonacci Numbers!【快速模幂+数列模除】

    The i'th Fibonacci number f(i) is recursively defined in the following way: • f(0) = 0 and f(1) = 1 ...

  8. HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】(废除!!!)

    本文废除,参见下述链接. 参考链接:HDU1163 Eddy's digital Roots[快速模幂+九余数定理+水题] 问题链接:HDU1163 Eddy's digital Roots. 问题简 ...

  9. POJ1845 Sumdiv【快速模幂+素因子分解+等比数列+二分法】

    问题链接:POJ1845 Sumdiv. 问题简述:参见上述链接. 问题分析:计算a^b的因子数,首先要对a进行因子分解,然后再进行计算. 程序说明:计算过程中用到了快速模幂函数. 题记:(略) AC ...

最新文章

  1. 使用for循环遍历文件
  2. 学习 ASP.NET mvc 第一天、也可能是最后一天
  3. Springboot-添加对jsp支持
  4. lda 可以处理中文_LDA数学八卦索引及全文文档
  5. 数据库练习(二)三个数据库根据指定id获取name和存储数据库名称
  6. STL中vector和list的区别
  7. .NET字符串格式化的几种方法及@符号的使用
  8. 六步搞定RHEL5下的mysql镜像数据库配置
  9. PL/SQL(一)简介
  10. sql server安装-没有权限访问文件
  11. 异速联:解决打印样式个性化设置
  12. 微信开发者工具 公众号网页调试的调试器没了?
  13. 审稿意见的“so what”如何处理?
  14. html文字旋转以后变形,CSS3中的变形处理——transform功能(旋转、缩放、倾斜、移动)...
  15. php 使用手机扫条码,关于扫条码的问题
  16. 【Ping检测】使用Ping命令检查网络连接情况
  17. 射击末世--建造者模式
  18. 一个动画看懂网络原理之CSMA/CD的工作原理
  19. cocos creator 3D学习(六)光照+阴影
  20. 最佳光圈值 (Optimum Aperture)

热门文章

  1. 大神对飞控精准高度估计算法解读
  2. 第十一届河南省赛--山区修路
  3. C++ remove、remove_copy、remove_if和remove_copy_if函数使用详解
  4. bit索引 mysql_Mysql优化之索引实现原理
  5. [Bugku CTF——Pwn] pwn4
  6. [软件推荐]使用OneNote来构建你自己的知识库(OFFICE2010)
  7. ant的if-else
  8. IIS的安装和配置全过程
  9. Java线程之守护线程(Daemon) .
  10. java中的null类型---有关null的9件事