HOJ 2901 Calculation 快速幂裸模板题
2013-01-18 08:43
204 查看
http://acm.hit.edu.cn/hoj/problem/view?id=2901
HOJ 2901 Calculation
My Tags | 快速幂 (Edit) |
---|
Source : GTmac | |||
Time limit : 1 sec | Memory limit : 32 M |
Given two integers a and b, you are to calculate the value of the following expression: (1b + 2b + ... + ab) modulo a.
Note that b is an odd number.
Input specifications
Each line of input consists of two integers a and b (1≤a≤1000000000, 1≤b≤1000000000) respectively.
Output specifications
For each case of input, you should output a single line, containing the value of the expression above.
Sample Input
1 1 2 1
Sample Output
0 1
/*
* 题目:
* ( 1^b + 2^b + ... + a^b ) % a
* 分析:
* 我们可以发现 a | [ i^b + (a-i)^b ],所以当a为奇数的时候可以直接快速幂,偶数的时候为0
*
* */
#include <cstdio> #include <iostream> using namespace std; typedef long long ll; ll cal(ll a,ll b,ll mod){ ll res = 1; while(b>0){ if(b&1) res = res*a%mod; a = a*a%mod; b >>= 1; } return res; } int main(){ ll a,b; while(cin>>a>>b){ if(a&1) puts("0"); else cout<<cal(a/2,b,a)<<endl; } return 0; }
相关文章推荐
- [数论]HOJ 2901 Calculation 快速幂
- [hoj 2901]Calculation[二项式定理]
- 矩阵快速幂模板
- POJ3070(矩阵快速幂模板)
- 矩阵快速幂模板
- Expression Blend实例中文教程(12) - 样式和模板快速入门Style,Template
- 业余爱好者如何通过使用模板快速建站1(How do amateurs build their own websites with a template part-1)
- hdu 4965 Fast Matrix Calculation(矩阵快速幂)
- WeiPHP插件模板中快速引入公共模板文件
- 洛谷4717:【模板】 快速沃尔什变换——题解
- MS CRM 2011 插件(plugin)的快速开发 -- 创建模板
- Android studio 快速代码模板生成常用的缩写汇总
- Expression Blend实例中文教程(12) - 样式和模板快速入门Style,Template
- 矩阵快速幂模板
- 【模板】快速幂
- 用模板写快速排序-数组
- 矩阵快速幂模板
- Additions HNUST 1713(矩阵快速幂模板 )
- 详解 Eclipse 中的快速 Java 编码(代码模板)
- 快速幂取模模板