uva 12253,12253

來源:互聯網
上載者:User

uva 12253,12253

題目連結;uva 12253 - Simple Encryption

題目大意:給定K1,求一個12位的K2,使得KK21=K2%1012

解題思路:按位枚舉,不且借用用快速冪模數判斷結果。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const ll ite=(1<<20)-1;ll N;/*ll mul_mod (ll x, ll n, ll mod) {    ll ans = 0;    x %= mod;    n %= mod;    while (n) {        if (n&1)            ans = (ans + x) % mod;        x = 2 * x % mod;        n >>= 1;    }    return ans;}*/ll mul_mod(ll a, ll b, ll mod) {    return (a * (b>>20) % mod * (1ll<<20) %mod + a*(b&(ite)) % mod) % mod;}ll pow_mod (ll x, ll n, ll mod) {    ll ret = 1;    while (n) {        if (n&1)            ret = mul_mod(ret, x, mod);        x = mul_mod(x, x, mod);        n >>= 1;    }    return ret;}bool dfs (int d, ll u, ll mod) {    if (d == 12) {        if (u >= 1e11 && pow_mod(N, u, mod) == u) {            printf("%lld\n", u);            return true;        }        return false;    }    for (int i = 0; i < 10; i++) {        if (pow_mod(N, i * mod + u, mod) == u) {            if (dfs(d+1, i * mod + u, mod * 10))                return true;        }    }    return false;}int main () {    int cas = 1;    while (scanf("%lld", &N) == 1 && N) {        printf("Case %d: Public Key = %lld Private Key = ", cas++, N);        for (int i = 0; i < 10; i++) {            if (dfs(1, i, 10))                break;        }    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.