HDU-4712 Hamming Distance【运气题 Rand()测试法】
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4712
题意:求n对中选string A 和 B 二进制xor后最少的 ‘1’ 的个数
题解:模拟TLE,打表可以减少复杂度,小伙伴跟我说米勒罗宾测试法可以试试,但是没敢用
/*米勒罗宾素数测试
Miller_Rabin */
LL random(LL n){...}
LL multi()//a*b%m
LL quick_mod()
bool miller_rabin(LL n){
for(L i=1;i<=n;i++){
LL a=random(n-2)+1;
if(quick_mod(a,n-1,n)!=1) return false;
}
return true;
}
简单的说,就是rand()随机取任意两组,当取得次数很大就越来越接近这个数,正确的概率很大。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long LL;
typedef long double LD;
const int maxn = 100100;
int n,t,mi;
char s[maxn][10];
//0-16任意选两个xor之后'1'的个数
int a[16][16]={ 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
1,0,2,1,2,1,3,2,2,1,3,2,3,2,4,3,
1,2,0,1,2,3,1,2,2,3,1,2,3,4,2,3,
2,1,1,0,3,2,2,1,3,2,2,1,4,3,3,2,
1,2,2,3,0,1,1,2,2,3,3,4,1,2,2,3,
2,1,3,2,1,0,2,1,3,2,4,3,2,1,3,2,
2,3,1,2,1,2,0,1,3,4,2,3,2,3,1,2,
3,2,2,1,2,1,1,0,4,3,3,2,3,2,2,1,
1,2,2,3,2,3,3,4,0,1,1,2,1,2,2,3,
2,1,3,2,3,2,4,3,1,0,2,1,2,1,3,2,
2,3,1,2,3,4,2,3,1,2,0,1,2,3,1,2,
3,2,2,1,4,3,3,2,2,1,1,0,3,2,2,1,
2,3,3,4,1,2,2,3,1,2,2,3,0,1,1,2,
3,2,4,3,2,1,3,2,2,1,3,2,1,0,2,1,
3,4,2,3,2,3,1,2,2,3,1,2,1,2,0,1,
4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0};
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",s[i]);
for(int j=0;j<=4;j++){
if(s[i][j] =='A')s[i][j] = 10 +'0';
if(s[i][j] =='B')s[i][j] = 11 +'0';
if(s[i][j] =='C')s[i][j] = 12 +'0';
if(s[i][j] =='D')s[i][j] = 13 +'0';
if(s[i][j] =='E')s[i][j] = 14 +'0';
if(s[i][j] =='F')s[i][j] = 15 +'0';
}
}
int ans,temp,sum,res;
mi = 50;
for(int m=0;m<1000000;m++){//次数
int i = rand()%n;
int j = rand()%n;
if(i==j) continue;
int res = 0;
for(int k=0;k<5;k++){
res += a[(s[i][k]-'0')][ (s[j][k]-'0')];
}
if(mi>res) {
mi = res;
}
}
printf("%d\n",mi);
}
return 0;
}