結果

問題 No.1559 Next Rational
ユーザー tassei903tassei903
提出日時 2021-03-04 14:39:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 925 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 11,000 KB
実行使用メモリ 8,268 KB
最終ジャッジ日時 2023-09-07 13:52:07
合計ジャッジ時間 1,199 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,224 KB
testcase_01 AC 16 ms
8,136 KB
testcase_02 AC 15 ms
8,224 KB
testcase_03 AC 15 ms
8,148 KB
testcase_04 AC 15 ms
8,036 KB
testcase_05 AC 15 ms
8,268 KB
testcase_06 AC 14 ms
8,236 KB
testcase_07 AC 14 ms
8,256 KB
testcase_08 AC 15 ms
8,112 KB
testcase_09 AC 14 ms
8,064 KB
testcase_10 AC 14 ms
8,096 KB
testcase_11 AC 16 ms
8,124 KB
testcase_12 AC 15 ms
8,240 KB
testcase_13 AC 15 ms
8,128 KB
testcase_14 AC 15 ms
8,260 KB
testcase_15 AC 14 ms
8,116 KB
testcase_16 AC 14 ms
8,112 KB
testcase_17 AC 15 ms
8,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,a,b,k=map(int,input().split())
if not 1<=n<=10**18:exit()
if not 1<=a<=10**9:exit()
if not 1<=b<=10**9:exit()
if not 1<=k<=10**9:exit()
m=10**9+7
s = (a*a+b*b+k)%m
s = (s*pow(a,-1,m))%m
s = (s*pow(b,-1,m))%m
z=[[s,-1],[1,0]]
mod=10**9+7
def ff(a,b):
    return a[0]*b[0]+a[1]*b[1]
#a*bの計算
def mat_mul(a,b):
    n,m,l=len(a),len(b[0]),len(b)
    c=[[0]*m for _ in range(n)]
    for i in range(n):
        for k in range(l):
            for  j in range(m):
                c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod
    return c
 
#a^nの計算
def mat_pow(a,n):
    m=len(a)
    b=[[0]*m for _ in range(m)]
    for i in range(m):
        b[i][i]=1
    while n>0:
        if n&1:
            b=mat_mul(b,a)
        a=mat_mul(a,a)
        n>>=1
    return b
#print(z)
if n>=3:
    ans=mat_pow(z,n-2)
    print(ff(ans[0],[b,a])%mod)
    #print(ans)
    #print(b)
else:
    if n==1:
        print(a)
    else:
        print(b)
0