結果

問題 No.16 累乗の加算
ユーザー jjjj
提出日時 2016-09-12 21:53:33
言語 Fortran
(gFortran 13.2.0)
結果
WA  
実行時間 -
コード長 876 bytes
コンパイル時間 1,312 ms
コンパイル使用メモリ 31,488 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-04-28 12:35:14
合計ジャッジ時間 2,013 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

program main
  implicit none
  integer*8::x,N,total,i
  integer*8,allocatable::a(:)
  integer::memo(0:100000000)
  data memo/100000001*0/,total/0/
  read *,x,N
  allocate(a(N))
  read *,a
  do i=1,N
     total = total + power(x,a(i),memo)
  end do
  print '(i0)',total
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(p.eq.0) then
       y = 1
       memo(p) = y
    else if(p.eq.1) then
       y = x
       memo(p) = y
    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 if
  end function power
end program main
0