結果

問題 No.577 Prime Powerful Numbers
ユーザー cielciel
提出日時 2017-10-14 12:52:06
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 768 bytes
コンパイル時間 72 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-04-28 22:02:11
合計ジャッジ時間 4,279 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/ruby
class Integer
	def pow_binary_mod(y,m)
		x = self
		z = 1
		while y!=0
			z=z*x%m if y&1!=0
			x=x*x%m
			y>>=1
		end
		z
	end
	def miller_rabin
		n=self #.abs #todo#
		return true if n==2
		return false if n==1||n%2==0
		d=n-1
		s=0
		while d%2==0
			d/=2
			s+=1
		end
		a=1
		2.times{|_| #todo#
			a+=1
			a+=1 while a.gcd(n)!=1 #todo#
			r=a.pow_binary_mod(d,n)
			next if r==1||r==n-1
			if s.times.none?{|j|
				r=r.pow_binary_mod(2,n)
				r==n-1
			}
				return false
			end
		}
		return true
	end
end

def solve(n)
	return n>2 ? :Yes : :No if n%2==0
	m=2
	while m<n
		z=n-m;
		(1..60).each{|k|
			r=(1..z+1).bsearch{|e|e**k>z}-1
			next if m+r**k<n
			return :Yes if r.miller_rabin
		}
		m*=2
	end
	:No
end
gets.to_i.times{puts solve gets.to_i}
0