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 <cstdlib>
#include <cmath>
#include <algorithm>
#define SMod 1000000000000
#define ll long long
using namespace std;
#define N 10007 ll K1,K2;
long long mul(long long a,long long b,long long mod) {
ll ite = (1LL<<)-;
return (a*(b>>)%mod*(1ll<<)%mod+a*(b&(ite))%mod)%mod;
} ll fastm(ll a,ll b,ll m) {
ll res = 1LL;
while(b) {
if(b&1LL) res = mul(res,a,m);
a = mul(a,a,m);
b >>= ;
}
return res;
} ll wei[],ans; bool dfs(int c,ll now) {
if(c == ) {
if(now >= wei[]) { ans = now; return true; }
return false;
}
ll W = wei[c];
for(ll i=;i<=;i++) {
ll tmp = W*i+now;
if(fastm(K1,tmp,W) != tmp%W) continue;
if(dfs(c+,tmp)) return true;
}
return false;
} int main()
{
int t,cs = ;
wei[] = 1LL;
for(int i=;i<=;i++) wei[i] = wei[i-]*10LL;
while(scanf("%lld",&K1)!=EOF && K1) {
dfs(,);
printf("Case %d: Public Key = %lld Private Key = %lld\n",cs++,K1,ans%SMod);
}
return ;
}
还有一种循环迭代的方法,随机选取一个超过10^12的数,如1000000000007,将其代入计算,如果f(x)!=x,那么令x=f(x),如此循环,能在短时间内找出合法解。不知道为啥。。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define SMod 1000000000000
#define ll long long
using namespace std; ll K1,K2;
long long mul(long long a,long long b,long long mod) {
ll ite = (1LL<<)-;
return (a*(b>>)%mod*(1ll<<)%mod+a*(b&(ite))%mod)%mod;
} ll fastm(ll a,ll b,ll m) {
ll res = 1LL;
while(b) {
if(b&1LL) res = mul(res,a,m);
a = mul(a,a,m);
b >>= ;
}
return res;
}
ll f(ll x) {
return fastm(K1,x,SMod);
}
ll gao(ll x) {
while() {
ll fx = f(x);
if(fx == x) return x;
x = fx;
}
} int main()
{
int t,cs = ;
while(scanf("%lld",&K1)!=EOF && K1) {
printf("Case %d: Public Key = %lld Private Key = %lld\n",cs++,K1,gao());
}
}
UVALive 4998 Simple Encryption --DFS的更多相关文章
- UVALive 4998 Simple Encryption
题目描述: 输入正整数K1(K1<=5000),找一个12位正整数K2使得K1K2=K2(mod 1012). 解题思路: 压缩映射原理:设X是一个完备的度量空间,映射ƒ:Χ→Χ 把每两点的距离 ...
- uva 12253 - Simple Encryption(dfs)
题目链接:uva 12253 - Simple Encryption 题目大意:给定K1.求一个12位的K2,使得KK21=K2%1012 解题思路:按位枚举,不且借用用高速幂取模推断结果. #inc ...
- LA 4998 Simple Encryption
题意:输入正整数$K_1(K_1 \leq 50000)$, 找一个$12$位正整数$K_2$(不能含有前导零)使得${K_1}^{K_2}\equiv K_2(mod10^{12})$. 例如,$K ...
- UVALive 4997 ABCD Tiles --DFS
题意: NxN的地图,上面有A颜色的瓷砖以及一些空格点,要用B,C,D颜色去填充这些空格,只能十字形的填充,还要保证共角或共边的格子不能是相同颜色,求一种字典序最小的填充方法,如果不能,输出" ...
- UVALive 6450 Social Advertising DFS解法
题意:一些人有朋友关系,在某个人的社交网站上投放广告可以被所有该人的直接朋友看到,问最小投放多少个广告使给出的人都看到广告.(n<=20) 解法:看到n的范围可以想到用二进制数表示每个人被覆盖与 ...
- UVALive 5881 Unique Encryption Keys (DP)
Unique Encryption Keys 题目链接: http://acm.hust.edu.cn/vjudge/problem/26633 Description http://7xjob4.c ...
- UVA12253 简单加密法 Simple Encryption
这题到现在还是只有我一个人过?太冷门了吧,毕竟你谷上很少有人会去做往年ACM比赛的题 题面意思很简单,每次给出\(K_1\),让你求一个\(K_2\)满足\(K_1^{K_2}\equiv K_2(\ ...
- BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...
- UVALive 6527 Counting ones dfs(水
题目链接:点击打开链接 #include <cstdio> #include <vector> using namespace std; typedef long long l ...
随机推荐
- 编程模式之模板方法模式(Template Method)
模板方法模式由两个角色组成:父类角色,子类角色. 父类角色:提供模板. 子类角色:为父类模板提供实现. 类图: JAVA代码: AbstractClass.java package com.templ ...
- Spring2.0-applicationContext.xml中使用el表达式给实体类属性赋值被当成字符串-遁地龙卷风
(-1)写在前面 这两天读<javaweb开发王者归来>,学到Spring的PropertyPlaceholderConfigurer时出现一个问题,我已${jdbc.name}的形式赋值 ...
- 16、ASP.NET MVC入门到精通——MVC过滤器
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 在ASP.NET MVC中有四种过滤器类型
- Hibernate(十)__缓存机制
为什么需要缓存? 缓存的作用主要用来提高性能,可以简单的理解成一个Map: 使 用缓存涉及到三个操作:把数据放入缓存.从缓存中获取数据. 删除缓存中的无效数据. 从上图看出: 当我们去查询对象的时候, ...
- Code First :使用Entity. Framework编程(5) ----转发 收藏
第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...
- jQuery带tab切换搜索框样式代码
效果体验:http://hovertree.com/texiao/jquery/23/ 代码如下,保存到HTML文件也可以查看效果: <!DOCTYPE html> <html la ...
- VS中如何快捷地给自己的代码添加创建信息注释
VS中如何快捷地给自己的代码添加创建信息注释 Intro 以下讨论的都是没有使用 GIT 来管理源代码的情况,如果使用 GIT 管理源代码可直接使用VS的Git扩展就不需要考虑以下问题. 什么是创建信 ...
- 使用私有Pod Spec的类库--提高公司开发效率
前言 找了这么长时间,再次开始去尝试Cocoapods了.前面已经写过一篇关于如何把自己的Github上的代码库添加Cocoapods支持.现在就让我们看一下如果搭建私有的Spec吧. 之所以构建私有 ...
- iOS JsonModel 的使用
本文转自:http://blog.csdn.net/smking/article/details/40432287 下面讲一下JSONModel的使用方法. @inteface MyModel : J ...
- C语言关于利用sscanf实现字符串相加减
#include<stdio.h>#include<string.h>void main(){ int a; int b; char str1[10] = "9999 ...