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 ...
随机推荐
- html5数字和颜色输入框
html5功能强大,数字和颜色输入框例子 效果:http://hovertree.com/code/html5/rxujb6g8.htm <!DOCTYPE html> <html& ...
- C#+arcengine10.0+SP5实现鹰眼(加载的是mdb数据库中的数据)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C#中AppDomain.CurrentDomain.BaseDirectory及各种路径获取方法
// 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径,包含文件名System.Diagnostics.Proces ...
- Create function through MySQLdb
http://stackoverflow.com/questions/745538/create-function-through-mysqldb How can I define a multi-s ...
- 通过Laravel 初识Vue.js
最近也在学习laravel的框, 因为之前学过tp框架, 都说laravel是最优雅的框架,所以开学后忍不住去试试这个在国外已经火的不要不要的框架. 总的来说,对于学习完tp框架后,我觉得tp毕竟是中 ...
- [ASP.NET Core] Middleware
前言 本篇文章介绍ASP.NET Core里,用来处理HTTP封包的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 在ASP.NET Core ...
- Promise和$.Deferred总结
语法对比: Promise .then(f).catch(f)是.then(f,f)的语法糖 .all([A,B,C])等最慢的 .race([A,B,C])最快的 $.Deferred .d ...
- js自建方法库(持续更新)
1.得到一个数,在一个有序数组中应该排在的位置序号: function orderInArr(num,arr) { if(num > arr[0]){ return 1 + arguments. ...
- 基于WebGL的三维地形渲染
1.生成WebMap页面 #!/usr/bin/env python # -*- coding: utf-8 -*- import subprocess from jinja2 import Envi ...
- SharePoint 2013 搭建app本地开发环境
使用SharePoint App,如果要通过应用程序目录分发 SharePoint 相关应用程序,如具有完全控制权限的 SharePoint 相关应用程序(无法部署到 Office 365 网站),则 ...