F - Prime Path

题目大意:
素数路径
估计看数据就明白这道题什么意思了......给两个素数,都是四位数的素数,并且没有前导0,现在需要经过一种变换把一个素数转换成另一个,当然这种转换是有规则的,规则就是每次只能改变这个四位数的其中一位数字,当然改变后的数字也得是素数,问最少的改变次数是多少......
貌似还是广搜..............................................................................................不过做起来应该会麻烦点,要求素数,不过可以搞一个素数表这样判断起来更方便

 

#include<stdio.h>
#include< string.h>
#include<math.h>
#include<queue>
using  namespace std;

#define maxn 10000

int p[maxn]; // 标记4位数的素数

int Prime( int n)
{
     int i, k=sqrt(n);

     for(i= 2; i<=k; i++)
         if(n % i ==  0)
             return  0;

     return  1;
}
int Turn( int n,  int k) // 把n的第k位转换成0
{
     char s[ 10]={ 0};

    sprintf(s,  " %d ", n);
    s[k] =  ' 0 ';
    sscanf(s,  " %d ", &n);

     return n;
}
int BFS( int s,  int e)
{
     int i, j, k, q;
     int v[maxn]={ 0};
    queue< int> Q;

    Q.push(s);
    v[s] =  1;

     while(Q.size())
    {
        s = Q.front();Q.pop();

         if(s == e)
             return v[s]- 1;

         int t =  1000;

         for(i= 0; i< 4; i++)
        {
            q = Turn(s, i);

             for(k= 0; k< 10; k++)
            {
                j = q+k*t;

                 if(p[j] ==  1 && v[j] ==  0)
                {
                    Q.push(j);
                    v[j] = v[s] +  1;
                }
            }

            t /=  10;
        }
    }

     return - 1;
}

int main()
{
     int i, s, e, T;

     for(i= 1000; i<maxn; i++)
        p[i] = Prime(i);

    scanf( " %d ", &T);

     while(T--)
    {
        scanf( " %d%d ", &s, &e);

         int ans = BFS(s, e);

         if(ans == - 1)
            printf( " Impossible\n ");
         else
            printf( " %d\n ", ans);
    }

     return  0;

} 

转载于:https://www.cnblogs.com/liuxin13/p/4648717.html

转python写法:#!/bin/sh time_stamp=`date +%s` function CheckStop() { if [ $? -ne 0 ]; then echo "execute fail, error on line_no:"$1" exit!!!" exit fi } function GenEcdsaKey() { ec_param_file_path="/tmp/ec_param.pem."$time_stamp openssl ecparam -out $ec_param_file_path -name prime256v1 -genkey CheckStop $LINENO openssl genpkey -paramfile $ec_param_file_path -out $1 CheckStop $LINENO openssl pkey -in $1 -inform PEM -out $2 -outform PEM -pubout CheckStop $LINENO rm $ec_param_file_path echo "gen_ecdsa_key succ prikey_path:"$1" pubkey_path:"$2 } function GenEcdsaSign() { ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp echo -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENO openssl pkeyutl -sign -in $ec_sign_info_sha256 -out $ec_binary_sign_file -inkey $3 -keyform PEM CheckStop $LINENO openssl base64 -e -in $ec_binary_sign_file -out $4 CheckStop $LINENO rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file echo "gen_ecdsa_sign succ sign_file_path:"$4 } function VerifyEcdsaSign() { ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp echo -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENO openssl base64 -d -in $4 -out $ec_binary_sign_file CheckStop $LINENO openssl pkeyutl -verify -in $ec_sign_info_sha256 -sigfile $ec_binary_sign_file -pubin -inkey $3 -keyform PEM rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file } function Usage() { echo "Usage:" echo "mmiot_ecdsa_sign.sh gen_ecdsa_key <private_key_file_path> <public_key_file_path>" echo "mmiot_ecdsa_sign.sh gen_ecdsa_sign <product_id> <sn> <private_
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值