Avionics OJ-2035 people to see people love a^b

Source: Internet
Author: User

People See people love a^b

Problem Description An integer representing the last three digits of the a^b. Description: The meaning of A^b is "A's B-square"

Input data contains multiple test instances, one row per instance, consisting of two positive integers a and B (1<=a,b<=10000), if a=0,
B=0, it indicates the end of the input data and does not handle it.

Output for each test instance, print the last three bits of the a^b that represent the integers, one row for each output.

Sample Input

2 3 12 6 6789 10000 0 0

Sample Output

8 984 1

A topic of the same remainder theorem , water problem.
Congruence theorem:
The mathematical notation is:
A≡b (mod D)
Its meaning is a mod d = b mod D
Here are the seven laws of the congruence theorem
1) a≡a (mod d)
2) a≡b (mod d) →b≡a (mod d)
3) (A≡b (mod D), b≡c (mod D)) →a≡c (mod d)
If A≡x (mod d), b≡m (mod d), then
4) a+b≡x+m (mod d)
5) a-b≡x-m (mod d)
6) a*b≡x*m (mod d)
7) a≡b (mod D) is a-B divisible by D

Then for
(A * b) (mod c) = (x * y) (mod c)
x = a mod c
y = b mod C

So for (a^b) MoD C in this form, set the mid-volume mid, the initial value is 1, then perform a B-operation can be obtained the answer:
MID = (Mid * (a mod c)) MoD C

The following is the implementation code:

#include <stdio.h>#define MOD(i) (i) % 1000int main(){    int a,b;    while(scanf("%d%d",&a,&b) != EOF && a | b){        int0;        int mul_mod_value = MOD(a);        while(b--) ans = MOD(ans * mul_mod_value);        printf("%d\n",ans);    }    return0;}

Avionics OJ-2035 people to see people love a^b

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.