QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#722563#249. Miller Rabin 算法AutomatiC__WA 74ms3612kbC++231.5kb2024-11-07 19:33:282024-11-07 19:33:29

Judging History

This is the latest submission verdict.

  • [2024-11-07 19:33:29]
  • Judged
  • Verdict: WA
  • Time: 74ms
  • Memory: 3612kb
  • [2024-11-07 19:33:28]
  • Submitted

answer

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cstdlib>
#include <bitset>
#include <deque>
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3fll
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}

ll bmul(ll a, ll b, ll m) {  // 快速乘
  ull c = (ull)a * (ull)b - (ull)((long double)a / m * b + 0.5L) * (ull)m;
  if (c < (ull)m) return c;
  return c + m;
}

ll qpow(ll x, ll p, ll mod) {  // 快速幂
  ll ans = 1;
  while (p) {
    if (p & 1) ans = bmul(ans, x, mod);
    x = bmul(x, x, mod);
    p >>= 1;
  }
  return ans;
}

bool Miller_Rabin(ll p) {  // 判断素数
  if (p < 2) return false;
  if (p == 2) return true;
  if (p == 3) return true;
  ll d = p - 1, r = 0;
  while (!(d & 1)) ++r, d >>= 1;  // 将d处理为奇数
  for (ll k = 0; k < 10; ++k) {
    ll a = rand() % (p - 2) + 2;
    ll x = qpow(a, d, p);
    if (x == 1 || x == p - 1) continue;
    for (int i = 0; i < r - 1; ++i) {
      x = x * x % p;
      if (x == p - 1) break;
    }
    if (x != p - 1) return false;
  }
  return true;
}
int main() {
	ll x;
	while (cin >> x) {
		if (Miller_Rabin(x)) {
			cout << "Y" << endl;
		} else {
			cout << "N" << endl;
		}
	}
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 74ms
memory: 3612kb

input:

996581938833575363
971646509461160317
773155992127361153
161603952726515268
540879144500456953
476831214764178553
784255927154201144
671096087619405061
805545190025269709
339546334309245137
337726347922962343
222956293307015293
809183111090275843
799050063298926344
691718101820598109
646220213113313...

output:

N
N
N
N
N
N
N
N
N
N
Y
N
Y
N
N
N
Y
Y
N
N
N
Y
N
N
Y
N
Y
N
N
N
N
N
N
N
N
N
Y
N
N
Y
Y
Y
Y
Y
N
N
N
N
N
Y
N
Y
N
Y
Y
Y
N
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
N
N
Y
N
N
N
Y
Y
N
Y
N
N
Y
Y
N
N
N
N
N
Y
Y
N
N
N
Y
N
N
N
N
Y
Y
N
Y
N
Y
N
N
Y
N
Y
N
N
Y
N
N
Y
N
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
N
Y
Y
N
Y
N
N
Y
N
N
Y
Y
Y
N
Y
Y
Y
N
N
Y
Y
N
Y
Y
Y
...

result:

wrong answer 2nd lines differ - expected: 'Y', found: 'N'