結果

問題 No.7 プライムナンバーゲーム
ユーザー jjjj
提出日時 2016-07-24 01:21:51
言語 Fortran
(gFortran 13.2.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,156 bytes
コンパイル時間 1,618 ms
コンパイル使用メモリ 35,188 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:01:25
合計ジャッジ時間 2,399 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

recursive function game(W,N,isPrime, PrimeList) result(iswin)
  implicit none
  integer,parameter::prime=1,nonprime=0
  integer,parameter::win=1,lose=0,unkown=-1
  integer::N,iswin,i,this
  integer,allocatable::W(:),isPrime(:),PrimeList(:)

  if(N.le.3) then
     iswin = lose
     return
  else if(W(N).ne.unkown) then
     iswin = W(N)
     return
  else
     iswin = lose
     do i=SIZE(PrimeList),1, -1
        this = PrimeList(i)
        if(N-this.le.1) cycle
        iswin = win - game(W,N-this, isPrime, PrimeList)
        W(N)  = iswin
        if(iswin .eq. win ) then
           return
        end if
     end do
     return
  end if

end function game

subroutine get_prime(isPrime,  PrimeList, N)
  implicit none
  integer::i,j,N
  integer,parameter::prime=1,nonprime=0,unkown=-1
  integer,allocatable::isPrime(:), PrimeList(:)
  integer,allocatable::numlist(:)

  isPrime=prime
  isPrime(1)=nonprime
  isPrime(2:3)=prime
  do i=2, N
     if(isPrime(i).eq.prime) then
        do j=2*i, N, i
           isPrime(j) = nonprime
        end do
     else
        do j=4, FLOOR(SQRT(REAL(i)))
           if(MOD(i,j).eq.0) then
              isPrime(i) = nonprime
              exit
           end if
        end do
     end if
  end do

  allocate(numlist(N))
  numlist=(/(i,i=1,N)/)
  PrimeList=pack(numlist,isPrime(numlist).eq.prime)

end subroutine get_prime

program main
  implicit none
  interface
     recursive function game(W,N,isPrime, PrimeList) result(iswin)
       integer::N,iswin
       integer,allocatable::W(:), isPrime(:), PrimeList(:)
     end function game
     subroutine get_prime(isPrime, PrimeList, N)
       integer::N
       integer,allocatable::isPrime(:),PrimeList(:)
     end subroutine get_prime
  end interface
  integer::N,iswin,i
  integer,parameter::win=1,lose=0,unkown=-1
  integer,allocatable::W(:),isPrime(:),PrimeList(:)


  read *,N
  allocate(W(N))
  allocate(isPrime(N))
  allocate(PrimeList(N))

  call get_prime(isPrime, PrimeList, N)

  W(2:N)=unkown
  W(0:3)=lose
  iswin = game(W,N,isPrime, PrimeList)

  if(iswin.eq.win) then
     print '(a)',"Win"
  else
     print '(a)',"Lose"
  end if
end program main
0