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;
}

uva 12253 - Simple Encryption(dfs)

时间: 2024-08-05 14:53:39

uva 12253 - Simple Encryption(dfs)的相关文章

UVALive 4998 Simple Encryption --DFS

题意: 给出K1,求一个12位数(不含前导0)K2,使得K1^K2 mod (10^12) = K2. 解法: 求不动点问题. 有一个性质: 如果12位数K2满足如上式子的话,那么K2%1,K2%10,K2%100,...,K2%10^12都会满足如上式子.那么我们可以dfs从后往前一个一个找出这个数的每一位. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdli

UVa 572 Oil Deposits(DFS)

 Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots.

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

Uva LA6450 Social Advertising DFS

You have decided to start up a new social networking company. Other existing popular social networksalready have billions of users, so the only way to compete with them is to include novel features noother networks have.Your company has decided to ma

UVa 208 消防车(dfs+剪枝)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=144 题意:给出一个n个结点的无向图以及某个结点k,按照字典序从小到大顺序输出从1到结点k的所有路径. 思路:如果直接矩阵深搜的话是会超时的,所以我们可以从终点出发,将与终点相连的连通块保存起来,这样dfs深搜时可以剪枝掉一些到达不了的点.只要解决了这个,dfs就是小问题. 这道题还有点坑的

UVA 1103 Ancient Messages (DFS)

起初学习dfs的时候 看不懂这个题目  回过头来今天看的时候思路相对比较清晰了.但还是想不到怎么处理 查询有多少个空白洞.并且一个图中还有好多个象形字符.网上思路是 在周围扩展一圈0,使得文字之外的所有0 都连通,一次dfs标记所有文字之外的0为-1.然后遍历图,发现1就沿着有1 的路径dfs2,在这个过程中,如果发现0,那么必然是文字内部的空洞,此时把空洞dfs 置为-1,标记以后不再走.那么发现几次0就有几个空洞在文字中.那么每一次的dfs2,就处理了一个象形文字.cnt的值就是空洞的个数即

UVA - 1103Ancient Messages(dfs)

UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study texts written in ancient languages.One such language, used in Egypt more than 3000 years ago, is based on characters called hieroglyphs.Figure C.1 shows

UVa 572 油田(DFS求连通块)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=513 终于开始接触图了,恩,开始接触DFS了,这道题就是求连通分量,比较简单. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int m, n; //记录连通块的数量 6 cha

uva 11218 KTV(DFS+回溯)

uva 11218 KTV One song is extremely popular recently, so you and your friends decided to sing it in KTV. The song has 3 characters, so exactly 3 people should sing together each time (yes, there are 3 microphones in the room). There are exactly 9 peo