結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2018-04-09 02:27:04 |
| 言語 | Fortran (gFortran 14.2.0) |
| 結果 |
AC
|
| 実行時間 | 2,856 ms / 9,973 ms |
| コード長 | 1,860 bytes |
| コンパイル時間 | 419 ms |
| コンパイル使用メモリ | 33,664 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-16 23:08:54 |
| 合計ジャッジ時間 | 7,726 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
module mdl
integer(8)::prime(12)=[2,3,5,7,11,13,17,19,23,29,31,37]
contains
function mul_mod(a,b,m)result(ret)
implicit none
integer(8),value::a,b,m
integer(8)::ret,tmp
ret=0
do while(b>1)
if(mod(b,2)==0)then
tmp=a
if(tmp>=m-a)tmp=tmp-m
a=a+tmp
b=b/2
else
if(ret>=m-a)ret=ret-m
ret=ret+a
b=b-1
end if
end do
if(ret>=m-a)ret=ret-m
ret=ret+a
end function
integer(8) function pow(a,n,p)
implicit none
integer(8),value::a,n,p
a=mod(a,p)
pow=1
do while(n>0)
if(mod(n,2)==1)pow=mul_mod(pow,a,p)
n=n/2
a=mul_mod(a,a,p)
end do
end function
logical function isPrime(a)
integer(8)::q,i,j,k,x,y,tmp
integer(8),value::a
logical::flag
isPrime=.true.
if(a==2)return
if(a==1.or.mod(a,2)==0)then
isPrime=.false.
return
end if
q=a-1
do while(mod(q,2)==0)
q=q/2
end do
do i=1,size(prime)
x=pow(prime(i),q,a)
y=q
do while(y/=a-1.and.x/=1.and.x/=a-1)
y=y*2
x=mul_mod(x,x,a)
end do
if(x/=a-1.and.mod(y,2)==0)then
isPrime=.false.
return
end if
end do
end function
end module
program main
use mdl
implicit none
integer::n
integer(8)::i,j,k
integer(8),allocatable::x(:)
read*,n
allocate(x(n))
do i=1,n
read*,x(i)
if(isPrime(x(i)))then
print*,x(i),1
else
print*,x(i),0
end if
end do
end program