Hoj 2901 calculation quick power bare template question

Source: Internet
Author: User
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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.