HDU-4712 Hamming Distance【运气题 Rand()测试法】

10 篇文章 0 订阅

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值