Soluție HackerRank pentru Power Calculation, subdomeniul Algebra, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Power Calculation
- Domeniu: Algebra
- Limbaj: Python 3
Challenge: Power Calculation
Subdomeniu: Algebra (algebra)
Scor cont: 25.0 / 25
Submission status: Accepted
Submission score: 1.0
Submission ID: 464740284
Limbaj: python3
Link challenge: https://www.hackerrank.com/challenges/power-calculation/problem
Cerință
Help Shashank in calculating the value of S, which is given as follows. Since the value of S can be very large, he only wants the last 2 digits of S.
S = 1^N + 2^N + 3^N + ·s + K^N
Input Format
The first line contains an integer T i.e. number of the test cases.
The next T lines will each contain a pair of integers, i.e. K and N.
Output Format
Print the last two digits of S for each test case in separate lines.
Constraints
1 ≤ T ≤ 10^4
2 ≤ K ≤ 10^16
2 ≤ N ≤ 10^16
Sample Input#00
3
2 2
2 3
3 3
Sample Output#00
05
09
36
Sample Input#01
3
5 2
3 4
3 3
Sample Output#01
55
98
36
Explanation
For the first test case, 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55
Cod sursă
import sys
def main():
data = sys.stdin.buffer.read().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
k = int(data[idx]); n = int(data[idx + 1]); idx += 2
block = 0
for i in range(1, 101):
block = (block + pow(i, n, 100)) % 100
q, r = divmod(k, 100)
ans = (block * (q % 100)) % 100
for i in range(1, r + 1):
ans = (ans + pow(i, n, 100)) % 100
out.append(f"{ans:02d}")
sys.stdout.write('\n'.join(out))
if __name__ == '__main__':
main()
