Soluție HackerRank pentru Eugene and Big Number, subdomeniul Number Theory, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod…
- Problemă: Eugene and Big Number
- Domeniu: Number Theory
- Limbaj: Python 3
Challenge: Eugene and Big Number
Subdomeniu: Number Theory (number-theory)
Scor cont: 100.0 / 100
Submission status: Accepted
Submission score: 1.0
Submission ID: 464775242
Limbaj: python3
Link challenge: https://www.hackerrank.com/challenges/eugene-and-big-number/problem
Cerință
Eugene must do his homework, but he is struggling.
He has three integer numbers: *A*, *N*, *M*. He writes number *A* on the board *N* times in a row. Let's call the resulting big number *X*.
Help Eugene find *X* [modulo](https://en.wikipedia.org/wiki/Modulo_operation) *M*.
Input Format
First line contains *T*, the number of testcases.
Each testcase contains three numbers: *A*, *N*, *M* separated by a single space.
Output Format
Print the required answer for each testcase in a new line.
Constraints
+ 1 ≤ T ≤ 200
+ 0 ≤ A ≤ 10^3
+ 0 < N < 10^12
+ 1 < M < 10^9
Cod sursă
#!/bin/python3
import math
import os
import random
import re
import sys
def solve(a, n, m):
digits = len(str(a))
divisor = 10**digits-1
m *= divisor
return (a % m)*(pow(10, digits*n, m)-1) % m // divisor
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input().strip())
for t_itr in range(t):
first_multiple_input = input().rstrip().split()
a = int(first_multiple_input[0])
n = int(first_multiple_input[1])
m = int(first_multiple_input[2])
result = solve(a, n, m)
fptr.write(str(result) + '\n')
fptr.close()
