uva 12253 - Simple Encryption(dfs)

简介:

题目链接;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;
}






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5269717.html,如需转载请自行联系原作者
相关文章
|
6月前
|
流计算
POJ 3620--Avoid The Lakes【DFS】
POJ 3620--Avoid The Lakes【DFS】
30 0
UVa11565 - Simple Equations
UVa11565 - Simple Equations
52 0
|
人工智能 BI
UVa1554 - Binary Search
UVa1554 - Binary Search
49 0
UVa211 - The Domino Effect(DFS)
UVa211 - The Domino Effect(DFS)
69 0
LeetCode 67. Add Binary
给定两个二进制字符串,返回它们的总和(也是二进制字符串)。 输入字符串都是非空的,只包含字符1或0。
77 0
LeetCode 67. Add Binary
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
92 0
|
存储 算法
PAT (Advanced Level) Practice - 1151 LCA in a Binary Tree(30 分)
PAT (Advanced Level) Practice - 1151 LCA in a Binary Tree(30 分)
128 0
PAT (Advanced Level) Practice - 1138 Postorder Traversal(25 分)
PAT (Advanced Level) Practice - 1138 Postorder Traversal(25 分)
102 0
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
112 0