20251101 baekjoon

This commit is contained in:
songyc macbook 2025-11-01 22:01:06 +09:00
parent 86a01e2d17
commit 60de4d4536
3 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,27 @@
def matrix_multiply(A, B) :
B_T = list(zip(*B))
return [[sum(a*b for a,b in zip(row_A, col_B))%1000 for col_B in B_T] for row_A in A]
def power_matrix(A, B) :
if B == 1 :
return [[n % 1000 for n in row] for row in A]
half = power_matrix(A, B//2)
result = matrix_multiply(half, half)
if B%2==1 :
result = matrix_multiply(result, A)
return result
def print_matrix(A) :
for row in A :
print(*row)
N, B = map(int, input().split())
A = []
for _ in range(N) :
A.append(list(map(int,input().split())))
print_matrix(power_matrix(A, B))

View File

@ -0,0 +1,2 @@
while (n := list(map(int, input().split()))) != [0,0] :
print(sum(n))

View File

@ -0,0 +1,25 @@
export {};
const input = require("fs").readFileSync(0).toString().trim().split('\n');
const MOD = BigInt(1000000007);
const power = (num: bigint, n: bigint): bigint => {
if(n===BigInt(0)) return BigInt(1);
else if(n===BigInt(1)) return num%MOD;
const half = power(num, n/BigInt(2));
let result = (half * half) % MOD;
if(n%BigInt(2) === BigInt(1)) result = (result*num)%MOD;
return result;
}
let result = BigInt(0);
for(let line of input.slice(1)) {
const [N, S] = line.split(" ").map(BigInt);
result = (result + (S * power(N, MOD - BigInt(2))) % MOD) % MOD;
}
console.log(result.toString());