結果

問題 No.16 累乗の加算
ユーザー jjjj
提出日時 2016-09-12 21:56:04
言語 Fortran
(gFortran 13.2.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 796 bytes
コンパイル時間 1,396 ms
コンパイル使用メモリ 27,088 KB
実行使用メモリ 6,064 KB
最終ジャッジ日時 2023-09-08 12:01:29
合計ジャッジ時間 1,146 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 6 ms
6,064 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 5 ms
4,716 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 5 ms
5,320 KB
testcase_09 AC 5 ms
5,592 KB
testcase_10 AC 5 ms
4,844 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 5 ms
4,668 KB
testcase_13 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

program main
  implicit none
  integer*8::x,N,total,i
  integer*8,allocatable::a(:)
  integer::memo(0:100000000)
  integer,parameter::modulo=1000003
  data memo/100000001*0/,total/0/
  read *,x,N
  allocate(a(N))
  read *,a
  memo(0) = 1
  memo(1) = x
  do i=1,N
     total = total + power(x,a(i),memo)
  end do
  print '(i0)',MOD(total,modulo)
contains
  recursive function power(x,p,memo) result(y)
    integer*8::x,y
    integer*8::p
    integer,parameter::modulo=1000003
    integer::memo(0:100000000)

    if(memo(p).ne.0) then
       y = memo(p)
    else if(MOD(p,2).eq.0) then
       y = mod(power(x,p/2,memo)*power(x,p/2,memo),modulo)
       memo (p) = y
    else
       y = mod(power(x,p/2+1,memo)*power(x,p/2,memo),modulo)
       memo (p) = y
    end if
  end function power
end program
0