快速幂的研究
来源:互联网 发布:星际战甲漂亮捏脸数据 编辑:程序博客网 时间:2024/06/06 01:16
K Candy Description Alex has prepared n different candies. Before he distributes these candies to the children Alex would like to consider all his options. If there are m children in the school find out the number of ways Chef can distribute these n candies to m children. Since the number of ways to distribute cookies may be very large report the answer modulo 1e9+7.
Input The first line of input consists of T, the number of test cases. The next T lines consist of 2 integers each, n and m. 1 <=T <= 10^5 1<=n,m<=1e9
Output Print T lines with the answer for each case on a new line.
Sample Input 3 1 2 2 1 10 1
Sample Output 2 1 1
先简单介绍一下快速幂,举个例子。你要求n^m,你如何去写?
一个个乘?那么它的复杂度就是O(n)。有没有更快的方法呢?
我要求n^m,不妨先求n^(m/2),这样我一平方,就得到了n^m。m越大省略的步骤越多,对不对?
既然这样,我们根据极限的思想,不断二分,然后利用递归,便可将复杂度缩小到O(lgn)。当然其中会有奇偶性的细节性问题,这里先只说一个大体思想。
上面是例题,等明天我详细写一下。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
long long function( long long n, long long m){ long long tmp = m % mod; long long ret = 1; while(n){ if(n&1) ret = ret*tmp%mod; tmp *= tmp; n >>= 1; } return ret;具体函数的实现,很巧妙。想了很久没想出来。学习了。
- 快速幂的研究
- 简单快速的可用性测试【用户研究】
- 快速排序,终极研究
- 快速排序研究
- OpenGL内容快速研究
- Everything研究之快速获取USN记录的文件路径
- 如何快速开始本专业的新课题研究
- Everything研究之快速获取USN记录的文件路径
- 图像和视频的快速去雾算法研究
- 转:快速H.264编码算法的研究及实现
- 快速H.264编码算法的研究及实现
- 矩阵相关(研究总结,矩阵,矩阵快速幂)
- 算法研究之快速排序
- 算法研究之快速排序
- 算法研究之快速排序
- ggplot2-快速研究数据分布
- 边缘文本检测:快速的和健壮的场景文本定位算法的研究
- 字母A-Z的快速定位滑块的学习研究
- HDU 水题十道,慢慢品味
- Collection接口/List接口/Set接口知识点详解
- android核心基础day05
- window10安装MongoDB
- 关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation
- 快速幂的研究
- 如何阅读项目源代码
- 判断一个字符串的ip是否是IP合法的ip地址
- Android 新安全机制之动态权限申请
- 阶段总结--业务系统代码中常见的异常错误总结以及避免方法
- CentOS安装vritualbox增强功能
- linux命令学习
- GDB调试中将信号发送给程序的方法
- House Robber II