Http://acm.hit.edu.cn/hoj/problem/view? Id = 2901 Hoj 2901 Calculation
| My tags |
Quick power (edit) |
| |
Source: GTMAC |
| |
Time Limit: 1 sec |
|
Memory limit: 32 m |
Submitted312,Accepted105
Given two integers A and B, you are to calculate the value of the following expression: (1B + 2B +... + AB) modulo.
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 12 1
Sample output
01
/*
* Question:
* (1 ^ B + 2 ^ B +... + A ^ B) %
* Analysis:
* We can find that a | [I ^ B + (a-I) ^ B], so when a is an odd number, you can directly use the power, and when an even number is 0.
*
**/
# Include <cstdio> # include <iostream> using namespace STD; typedef 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 "); elsecout <CAL (A/2, B, A) <Endl ;}return 0 ;}