• Power of Cryptography


    Problem Description
    Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers among these primes. Work in this area has resulted in the practical use of results from number theory and other branches of mathematics once considered to be only of theoretical interest.  This problem involves the efficient computation of integer roots of numbers.  Given an integer n>=1 and an integer p>= 1 you have to write a program that determines the n th positive root of p. In this problem, given such integers n and p, p will always be of the form k to the nth. power, for an integer k (this integer is what your program must find).
     
    Input
    The input consists of a sequence of integer pairs n and p with each integer on a line by itself. For all such pairs 1<=n<= 200, 1<=p<10101 and there exists an integer k, 1<=k<=109 such that kn = p.
     
    Output
    For each integer pair n and p the value k should be printed, i.e., the number k such that k n =p.
     
    Sample Input
    2 16
    3 27
    7 4357186184021382204544
     
    Sample Output
    4
    3
    1234
     
    Source
    PKU
     

    解题思路:二分加高精度计算;

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 #include<string>
      5 
      6 
      7 using namespace std;
      8 
      9 
     10 const long jz=100000000;
     11 char s[105];
     12 long n,k;
     13 struct bignum{
     14        long long an[50];
     15        long l;
     16 };
     17 bignum b,tm;
     18 void cpy(bignum &a,bignum b){//赋值b给a
     19      long i;
     20      a.l=b.l;
     21      for (i=0;i<a.l;i++)
     22      a.an[i]=b.an[i];
     23 }
     24 void gjc(bignum &c,bignum a,bignum b){//计算a*b
     25     long i,j;
     26     memset(c.an,0,sizeof(c.an));
     27     c.l=0;//?????
     28     for (i=0;i<a.l;i++)
     29         for (j=0;j<b.l;j++){
     30             c.an[i+j]+=a.an[i]*b.an[j];
     31             c.an[i+j+1]+=c.an[i+j]/jz;
     32             c.an[i+j]%=jz;
     33         }
     34     c.l=20;
     35     while (c.an[c.l-1]==0) c.l--;
     36     return;
     37 }
     38 short cmp(bignum a,bignum b){//比较a和b
     39       long i;
     40       if (a.l>b.l) return 1;
     41       if (a.l<b.l) return -1;
     42       for (i=a.l-1;i>=0;i--){
     43           if (a.an[i]>b.an[i]) return 1;
     44           if (a.an[i]<b.an[i]) return -1;
     45       }
     46       return 0;
     47 }
     48 void qpow(bignum &c,bignum a,long b){//计算a(d)的b(p)次方;
     49     long i,p;
     50     bignum d;
     51     p=b;
     52     c.l=1;
     53     c.an[0]=1;
     54     cpy(d,a);
     55     while (p>0){
     56           if (p&1)
     57              gjc(c,c,d);
     58           if (c.l>15){
     59              cpy(c,tm);
     60              return;
     61           }
     62           gjc(d,d,d);//d进行平方
     63           if (d.l>15){
     64              cpy(c,tm);
     65              return;
     66           }
     67           p=p>>1;//奇数-1/2;偶数/2;
     68     }
     69 }
     70 void bcov(bignum &a,long b){//把b给a
     71      memset(a.an,0,sizeof(a.an));
     72      a.l=0;
     73      a.l=1;
     74      a.an[0]=b;
     75      if (a.an[0]>jz){
     76         a.l++;
     77         a.an[1]=a.an[0]/jz;
     78         a.an[0]%=jz;
     79      }
     80 }
     81 void convert(bignum &a){//开始的p输入;
     82      long i,j,l,k,t;
     83      l=strlen(s);
     84      j=1;k=0;t=0;a.l=0;
     85      for (i=l-1;i>=0;i--){
     86          k=k+j*(s[i]-'0');
     87          j*=10;
     88          if ((l-i)%8==0){
     89             j=1;
     90             a.an[a.l++]=k;
     91             k=0;
     92          }
     93      }
     94      if (k)
     95         a.an[a.l++]=k;
     96 }
     97 long divf(){
     98      long max,min,mid,tmp;
     99      bignum a;
    100      min=0;max=1000000000;//max=10亿
    101      while (min+1<max){//二分点
    102            mid=(max+min)>>1;
    103            bcov(a,mid);//把mid给a
    104            qpow(a,a,n);//计算a的n次方
    105            tmp=cmp(a,b);//比较a和b
    106            if (tmp==0) return mid;
    107            if (tmp<0) min=mid;
    108            if (tmp>0) max=mid;
    109      }
    110      bcov(a,max);//把max给a;
    111      qpow(a,a,n);//计算a的n次方
    112      if (cmp(a,b)>0)//如果a>b
    113         return min;
    114      else
    115          return max;
    116 }
    117 int main(){
    118     while (scanf("%ld %s",&n,s)==2){
    119           convert(b);//将b转换到b中
    120           tm.l=20;
    121           tm.an[19]=1;
    122           cout<<divf()<<endl;
    123     }
    124     return 0;
    125 }
    View Code
  • 相关阅读:
    这是一个关于Latex的测试
    在Mac下配置php开发环境:Apache+php+MySql
    CSS 颜色代码大全
    APP中关于Android和IOS与网页交互
    linux crontab
    dedecms中的内容页中的变量
    lamp中的Oracle数据库链接
    phpcms使用session的方法
    linux系统下nginx安装目录和nginx.conf配置文件目录
    window7+wamp环境配置Oracle数据库连接
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3224365.html
Copyright © 2020-2023  润新知