HDU 1097.A Hard Puzzle "fast power or regularity" "August 12"

Source: Internet
Author: User

A Hard Puzzle

Problem Descriptionlcy gives a hard puzzle to feng5166,lwg,jgshining and ignatius:gave A and b,how to know the A^b.everyb Ody objects to this BT problem,so LCY makes the problem easier than begin.
This puzzle describes That:gave A and b,how to know the A^b ' s, the last digit number. But everybody was too lazy to slove the problem,so they remit to you, who was wise.

Inputthere is mutiple test cases. Each test cases consists of numbers a and B (0<a,b<=2^30)

Outputfor Each test case, you should output of the a^b ' s last digit number.

Sample Input
7 668 800

Sample Output
96
title: Ask for the last number of a^b, the problem into a^b%10, the code is as follows:
#include <cstdio>int mod (int a,int b,int k) {    int ans=1;    A=a%k;    while (b>0) {        if (b%2==1) ans= (ans*a)%k;        b/=2;        A= (a*a)%k;    }    return ans;} int main () {    int a A, B;    while (scanf ("%d%d", &a,&b) ==2)        printf ("%d\n", mod (a,b,10));    return 0;}
This is a direct method of seeking. Look at the other people's puzzle, there is a law:
the mantissa is 0,1,5,6 regardless of how many times the mantissa remains unchanged, while the mantissa is 4 and 9 for every 2 cycles,
2,3,7,8 for every 4 cycles. The loop results are as follows:
0,1,5,6: The number of digits is always 0,1,5,6
2:6,2,4,8 Cycle
3:1,3,9,7 Cycle
4:6,4 Cycle
7:1,7,9,3 Cycle
8:6,8,4,2 Cycle
9:1,9 Cycle
Yes, I can get the water.the following:
#include <cstdio>int div[10]={1,1,4,4,2,1,1,4,4,2};int f[10][4]={{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6}, {1,7,9,3},{6,8,4,2},{1,9}};int main () {    int A, b;    while (scanf ("%d%d", &a,&b) ==2)        printf ("%d\n", f[a%10][b%div[a%10]]);    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1097.A Hard puzzle "fast power or Law" "August 12"

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.