题意: 给出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的更多相关文章

  1. UVALive 4998 Simple Encryption

    题目描述: 输入正整数K1(K1<=5000),找一个12位正整数K2使得K1K2=K2(mod 1012). 解题思路: 压缩映射原理:设X是一个完备的度量空间,映射ƒ:Χ→Χ 把每两点的距离 ...

  2. uva 12253 - Simple Encryption(dfs)

    题目链接:uva 12253 - Simple Encryption 题目大意:给定K1.求一个12位的K2,使得KK21=K2%1012 解题思路:按位枚举,不且借用用高速幂取模推断结果. #inc ...

  3. LA 4998 Simple Encryption

    题意:输入正整数$K_1(K_1 \leq 50000)$, 找一个$12$位正整数$K_2$(不能含有前导零)使得${K_1}^{K_2}\equiv K_2(mod10^{12})$. 例如,$K ...

  4. UVALive 4997 ABCD Tiles --DFS

    题意: NxN的地图,上面有A颜色的瓷砖以及一些空格点,要用B,C,D颜色去填充这些空格,只能十字形的填充,还要保证共角或共边的格子不能是相同颜色,求一种字典序最小的填充方法,如果不能,输出" ...

  5. UVALive 6450 Social Advertising DFS解法

    题意:一些人有朋友关系,在某个人的社交网站上投放广告可以被所有该人的直接朋友看到,问最小投放多少个广告使给出的人都看到广告.(n<=20) 解法:看到n的范围可以想到用二进制数表示每个人被覆盖与 ...

  6. UVALive 5881 Unique Encryption Keys (DP)

    Unique Encryption Keys 题目链接: http://acm.hust.edu.cn/vjudge/problem/26633 Description http://7xjob4.c ...

  7. UVA12253 简单加密法 Simple Encryption

    这题到现在还是只有我一个人过?太冷门了吧,毕竟你谷上很少有人会去做往年ACM比赛的题 题面意思很简单,每次给出\(K_1\),让你求一个\(K_2\)满足\(K_1^{K_2}\equiv K_2(\ ...

  8. BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...

  9. UVALive 6527 Counting ones dfs(水

    题目链接:点击打开链接 #include <cstdio> #include <vector> using namespace std; typedef long long l ...

随机推荐

  1. Android按返回键退出程序但不销毁,程序后台保留

    重写onKeyDown事件即可 @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyE ...

  2. 学习笔记之-------UIScrollView 基本用法 代理使用

    //contentSize.contentInset和contentOffset 是 scrollView三个基本的属性. // 滚动 self.ScrollView.contentSize =sel ...

  3. yii的入口文件index.php中为什么会有这两句

    yii的应用模板中,index.php中 前面会有这两句 <?php // comment out the following two lines when deployed to produc ...

  4. ASP.NET MVC搭建项目后台UI框架—9、服务器端排序

    ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NET M ...

  5. js自建方法库(持续更新)

    1.得到一个数,在一个有序数组中应该排在的位置序号: function orderInArr(num,arr) { if(num > arr[0]){ return 1 + arguments. ...

  6. Introduction to Microsoft Dynamics 365 licensing

    Microsoft Dynamics 365 will be released on November 1. In preparation for that, Scott Guthrie hosted ...

  7. 用Kotlin开发Android应用(III):扩展函数和默认值

    这是关于Kotlin的第三篇. 原文标题:Kotlin for Android (III): Extension functions and default values 原文链接:http://an ...

  8. iOS8 tableview separatorInset cell分割线左对齐,ios7的方法失效了

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -(void)viewDidLayoutSubviews {          if ([self.m ...

  9. Android Bitmap占用内存计算公式

    Android对各分辨率的定义 当图片以格式ARGB_8888存储时的计算方式 占用内存=图片长*图片宽*4字节 图片长 = 图片原始长 (设备DPI/文件夹DPI)  图片宽 = 图片原始宽(设备D ...

  10. 敏捷开发与jira之阶段工作项概述

    每次迭代都分这5个阶段,但每个阶段的时间根据版本情况定,最终目标是:第一个阶段拿到交付范围,在第五个阶段都完成,并拿到本次版本团队所消耗的工时. Jira是项目过程管理的一种手段,跟多体现在工时跟踪, ...