#洛谷P5535【XR-3】小道消息
##题意
给你一个n和k,其中n代表有n-1个人,其中这些人的衣服上有编号2-n。第0天你把小道消息给第k个人,也就是编号为k+1,第一天收到小道消息的人可以把小道消息传给衣服上的数字与传递小道消息的人的衣服上的数字互质的人,求最少需要多少天能把小道消息传给所有人?
##思路
当时看到题目的时候就想到了最大的次数就是2次,因为如果你第0天是奇数的话,那第一天所有穿偶数号码衣服的人都可以知道这个小道消息,那么第二天所有穿奇数号码衣服的人都可以知道这个小道消息,即所有人都可以知道这个小道消息
然后我就想到了质数问题,刚开始我就想到了如果第0天选的是一个质数,那么第1天所有人都可以知道这个小道消息,但是这样子第一个样例都过不了,因此我就想到了质数的倍数问题
题目中数据最大是1e14,所以我用了Miller Rabin的算法,但是一直有5个测试点没过,后面我用非常暴力的方法判断是否为素数就过了
如果有看到博客的人帮我看看我的Miller_Rabin代码错在哪
###没过的代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 30;
int modular_exp(int a, int m, int n) {
if (m == 0) {
return 1;
}
if (m == 1) {
return a % n;
}
int w = modular_exp(a, m / 2, n);
w = w * w % n;
if (m & 1)
w = w * a % n;
return w;
}
bool Miller_Rabin(int n) {
if (n == 2)
return true;
for (int i = 0; i < maxn; i++) {
int a = rand() % (n - 2) + 2;
if (modular_exp(a, n, n) != a) {
return false;
}
}
return true;
}
int main(void) {
#ifdef ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
srand(time(NULL));
ll n, k;
cin >> n >> k;
if (Miller_Rabin(k + 1)) {
if (n + 1 < (k + 1) * 2)
cout << '1' << endl;
else
cout << '2' << endl;
} else {
cout << '2' << endl;
}
}
###过了的代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 30;
ll modular_exp(ll a, ll m, ll n) {
if (m == 0) {
return 1;
}
if (m == 1) {
return a % n;
}
ll w = modular_exp(a, m / 2, n);
w = w * w % n;
if (m & 1)
w = w * a % n;
return w;
}
bool Miller_Rabin(ll n) {
if (n == 2)
return true;
for (int i = 0; i < maxn; i++) {
ll a = rand() % (n - 2) + 2;
if (modular_exp(a, n, n) != a) {
return false;
}
}
return true;
}
bool judge(ll x){
float n_sqrt;
for(auto i = 2; i <= (int)sqrt(x); i++){
if(x % i == 0){
return false;
}
}
return true;
}
int main(void) {
#ifdef ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//srand(time(NULL));
ll n, k;
cin >> n >> k;
if (judge(k + 1)) {
if ((n + 1) < (k + 1) * 2)
cout << '1' << endl;
else
cout << '2' << endl;
} else {
cout << '2' << endl;
}
}