結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
jj
|
| 提出日時 | 2016-07-24 01:43:38 |
| 言語 | Fortran (gFortran 14.2.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,213 bytes |
| コンパイル時間 | 770 ms |
| コンパイル使用メモリ | 35,216 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-06 15:41:03 |
| 合計ジャッジ時間 | 1,068 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 WA * 9 |
ソースコード
recursive function game(W,N,isPrime, PrimeList, numLessPrime) 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(:),numLessPrime(:)
if(W(N).ne.unkown) then
iswin = W(N)
return
else
iswin = lose
do i = numLessPrime(N),1, -1
this = PrimeList(i)
if(N-this.le.3) cycle
iswin = win - game(W,N-this, isPrime, PrimeList, numLessPrime)
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
do i=2, floor(SQRT(REAL(N)))
if(isPrime(i).eq.prime) then
do j=2*i, N, i
isPrime(j) = nonprime
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, numLessPrime) result(iswin)
integer::N,iswin
integer,allocatable::W(:), isPrime(:), PrimeList(:),numLessPrime(:)
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,total
integer,parameter::win=1,lose=0,unkown=-1
integer,allocatable::W(:),isPrime(:),PrimeList(:),numLessPrime(:)
integer,parameter::prime=1,nonprime=0
read *,N
allocate(W(N))
allocate(isPrime(N))
allocate(PrimeList(N))
allocate(numLessPrime(N))
call get_prime(isPrime, PrimeList, N)
W(0:3)=lose
W(4:N)=unkown
total=0
do i=1,N
numLessPrime(i)=total
if(isPrime(i).eq.prime) total=total+1
end do
iswin = game(W,N,isPrime, PrimeList, numLessPrime)
if(iswin.eq.win) then
print '(a)',"Win"
else
print '(a)',"Lose"
end if
end program main
jj