关于sqrtn 和 n / 2

公开20190226远古博客,汗

1. f(n)=\frac{n}{2}

while(n)

    n = f(n);

O(logn)

2. g(n)=\sqrt{n}

while(n >1)

    n = g(n);

O(loglogn)相当于对n的指数x做f(x)操作

 

#include <iostream> #include <cmath> #include <cstring> #include <stdio.h> using namespace std; //¼ÆËãa^b mod nµÄÖµ int modpow(int a, int b, int n){ int res = 1; while(b > 0){ if(b & 1){ res = (res * a) % n; } a = (a * a) % n; b >>= 1; } return res; } //ÅжÏÒ»¸öÊýÊÇ·ñΪËØÊý bool isPrime(int n){ if(n < 2) return false; int sqrtn = sqrt(n); for(int i = 2; i <= sqrtn; i++){ if(n % i == 0) return false; } return true; } //¼ÆËãÅ·À­º¯Êýphi(n)µÄÖµ int phi(int n){ int res = n; if(n % 2 == 0){ res /= 2; while(n % 2 == 0) n /= 2; } for(int i = 3; i <= sqrt(n); i += 2){ if(n % i == 0){ res = res / i * (i - 1); while(n % i == 0) n /= i; } } if(n > 1) res = res / n * (n - 1); return res; } //Éú³ÉRSAÃÜÔ¿¶Ô void genRSAKey(int &n, int &e, int &d){ int p, q; do{ p = rand() % 100 + 1; }while(!isPrime(p)); do{ q = rand() % 100 + 1; }while(!isPrime(q)); n = p * q; int phi_n = phi(n); do{ e = rand() % (phi_n - 2) + 2; }while(__gcd(e, phi_n) != 1); d = 1; while((d * e) % phi_n != 1){ d++; } } //RSA¼ÓÃÜ int RSAEncrypt(int m, int e, int n){ return modpow(m, e, n); } //RSA½âÃÜ int RSADecrypt(int c, int d, int n){ return modpow(c, d, n); } int main(){ int n, e, d; genRSAKey(n, e, d); //Éú³ÉRSAÃÜÔ¿¶Ô cout << "¹«Ô¿: (" << n << ", " << e << ")" << endl; cout << "˽Կ: (" << n << ", " << d << ")" << endl; int m; cout << "ÇëÊäÈëÒª¼ÓÃܵÄÃ÷ÎÄ: "; cin >> m; int c = RSAEncrypt(m, e, n); //¼ÓÃÜ cout << "¼ÓÃܺóµÄÃÜÎÄ: " << c << endl; int m2 = RSADecrypt(c, d, n); //½âÃÜ cout << "½âÃܺóµÄÃ÷ÎÄ: " << m2 << endl; return 0; }这段代码有错误的地方,请你解释并修改正确
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值