Atcoder Regular Contest 113 B-A^B^C

https://atcoder.jp/contests/arc113/tasks/arc113_b

Problem Statement:

Given positive integers A,B,C, find the digit at the ones place in the decimal notation of .

Constraints:


A,B,C are integers.

Inputs:

Input is given from Standard Input in the following format:

A B C

Outputs:

Print the digit at the one’s place in the decimal notation of .

Sample Input:

3141592 6535897 9323846

Sample Output:

2

分析:

数据很大,思路首先是使用快速幂,然后思考一下,输出的是结果%10的结果,而这个结果只与A的个位有关。

然后A % 10的幂也是循环的,发现对于0、1、5、6这样的值直接输出就可以,因为不管B^C为多少,都不会改变。然后4、9这样值循环大小为2,其余的为4。把循环大小作为B^C的模,这样能最好得优化。

B^C取模后会=0,这种情况要让他们恢复相应的循环的大小,因为A的0次幂肯定是0嘛。。要做这样一个判断。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>

using namespace std;

long long a,b,c;
long long A;


long long quick_pow(long long base,long long pow)
{
long long temp = 0;
if(A == 4 || A == 9)
{
temp = 2;
}
else
{
temp = 4;
}
long long ans = 1;
while(pow)
{
if(pow & 1)
{
ans = ans * base % temp;
}
base = base * base % temp;//caculate the x^n
pow >>= 1;
}
if(ans == 0)
{
if(A == 4 || A == 9)
{
ans = 2;
}
else
{
ans = 4;
}
}
return ans;
}

long long quick_pow2(long long base, long long p, long long m)
{
long long ans = 1 % m;
while(p)
{
if(p & 1)
{
ans = ans * base % m;
}
base = base * base % m;
p >>= 1;
}
return ans;
}

int main()
{
cin>>a>>b>>c;
A = a % 10;
if(A == 0 || A == 5 || A == 6 || A == 1)
{
cout<<A<<endl;
}
else
{
long long p = quick_pow(b, c);
cout<<quick_pow2(a, p, 10)<<endl;
}
return 0;
}