Decrypt Messages

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 657    Accepted Submission(s): 158

Problem Description
In the game BioHazard 4, the president's daughter has been abducted by some crazy villagers. Leon S. Kennedy, the secret agent of White House, goes to rescue her. He keeps in contact with Hunnigan, the president's secretary.

But the time in their contact log has been encrypted, using the following method:

Count the number of seconds from 2000.01.01 00:00:00 to that time, assume this number is x. Then calculate xq, modulo it by a prime number p. The remainder a is the encrypted number.

Your task is to help Leon write a program to decrypt the contact log, and tell him all the possible original time.

1. Remember that if the year can be divided evenly by 4 but can't be divided evenly by 100, or it can be divided evenly by 400, this year is a leap year. The February of a leap year has 29 days, while the February of other years has 28 days.

2. In this problem, if the year modulo 10 is 5 or 8, at the end of this year, there is one “leap second”, i.e., the second after 2005.12.31 23:59:59 is 2005.12.31 23:59:60, and after that second, it's 2006.01.01 00:00:00.
You may assume that from 2000.01.01 00:00:00 till that time, less than p seconds have passed.

 
Input
There are multiple test cases.
The first line of the input contains an integer T, meaning the number of the test cases.

For each test case, a single line of three integers: p, q, and a. (2<p≤1000000007, 1<q≤10, 0≤a<p, p is always a prime.)

 
Output
The time. If there are multiple solutions, you must output all possible solutions in chronological order.

If the solution doesn't exist, output Transmission error instead.
See the sample output for further details.

 
Sample Input
2
3 2 1
3 2 2
 
Sample Output
Case #1:
2000.01.01 00:00:01
2000.01.01 00:00:02
Case #2:
Transmission error
 
Source
 
Recommend
zhuweicong
题目大意:解方程:x^q = a (mop p),求从2000.01.01 00:00:00过x秒后的时间.
分析:综合性非常强的一道题,解这种n次剩余方程有固定的解法.
          先求出p的原根g,方法就是枚举g,然后检验它是不是原根,检验方法是枚举p-1的质因子a,如果g^((p-1)/a) = 1 (mod p),那么g就不是原根.然后令g^y = x,g^t = a,原方程就转化为yq = a (mod (p-1)).a是给定的,t可以用bsgs算出来.y就可以通过扩展欧几里得算法求出来了.因为题目要求所有解,所以最后要从最小正整数解通过枚举倍数来生成其他的解.
          在求解的时候需要及时地判断是否有解,最主要的判断是有没有原根,bsgs能不能求出解,扩欧能不能求出来.
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
typedef long long ll;
ll p,q,a,g,t;
int T,cas;
vector <ll> yinzi,endd;
map <ll,ll> bsg; ll qpow(ll a,ll b,ll mod)
{
ll res = ;
while(b)
{
if (b & )
res = (res * a) % mod;
a = (a * a) % mod;
b >>= ;
}
return res;
} ll exgcd(ll a,ll b,ll &x,ll &y)
{
if (!b)
{
x = ;
y = ;
return a;
}
ll temp = exgcd(b,a % b,x,y),t = x;
x = y;
y = t - (a / b) * y;
return temp;
} bool check(ll x)
{
ll t = p - ;
for (int i = ; i < yinzi.size(); i++)
if (qpow(x,t / yinzi[i],p) == )
return false;
return true;
} ll getyuangen()
{
ll x = p - ;
yinzi.clear();
for (ll i = ; i <= sqrt(x); i++)
if(x % i == )
{
yinzi.push_back(i);
while(x % i == )
x /= i;
}
if (x != )
yinzi.push_back(x);
ll temp = ;
while(++temp)
if (check(temp))
return temp;
} ll bsgs()
{
bsg.clear();
if (g % p == )
return -;
ll block = ceil(sqrt(p));
ll ans;
for (ll i = ; i <= block; i++)
{
if (i == )
{
ans = a % p;
bsg[ans] = i;
continue;
}
ans = (ans * g) % p;
bsg[ans] = i;
}
ll temp = qpow(g,block,p);
ans = ;
for (ll i = ; i <= block; i++)
{
ans = (ans * temp) % p;
if (bsg[ans])
{
ll anss = i * block - bsg[ans];
return (anss % p + p) % p;
}
}
return -;
} void solve()
{
endd.clear();
ll A = q,B = p - ,C = t,x,y;
ll d = exgcd(A,B,x,y);
if (C % d == )
{
x = (x % B + B) % B;
endd.push_back(x * (C / d) % B);
for (ll i = ; i < d; i++)
endd.push_back((endd[] + i * B / d) % B);
}
} int dd[],hh[],mi[];
int mm[]={,,,,,,,,,,,,};
void print(ll x){
int y,mo,d,h,min,s,i;
for(y=;;y++){
s=***;
if(y%==&&y%||y%==)
s+=**;
if(y%==||y%==)
s++;
if(x-s<)
break;
x-=s;
}
if(y%==&&y%||y%==)
for(i=;i<;i++)
mm[i]+=**;
if(y%==||y%==){
mm[]++;
dd[]++;
hh[]++;
mi[]++;
}
for(i=;i<;i++)
if(x-mm[i]<)
break;
x-=mm[i-];
mo=i;
for(i=;i<;i++)
if(x-dd[i]<)
break;
x-=dd[i-];
d=i;
for(i=;i<;i++)
if(x-hh[i]<)
break;
x-=hh[i-];
h=i-;
for(i=;i<;i++)
if(x-mi[i]<)
break;
x-=mi[i-];
min=i-;
if(y%==&&y%||y%==)
for(i=;i<;i++)
mm[i]-=**;
if(y%==||y%==){
mm[]--;
dd[]--;
hh[]--;
mi[]--;
}
printf("%d.%02d.%02d %02d:%02d:%02lld\n",y,mo,d,h,min,x);
} void init()
{
bsg.clear();
yinzi.clear();
endd.clear();
p = q = a = g = t = ;
} int main()
{
for(int i=;i<;i++)mm[i]=mm[i]***+mm[i-];
for(int i=;i<;i++)dd[i]=i***;
for(int i=;i<;i++)hh[i]=i**;
for(int i=;i<;i++)mi[i]=i*;
scanf("%d",&T);
while(T--)
{
++cas;
init();
scanf("%lld%lld%lld",&p,&q,&a);
printf("Case #%d:\n",cas);
if (a == )
{
printf("2000.01.01 00:00:00\n");
continue;
}
g = getyuangen();
t = bsgs();
if (t == -)
{
printf("Transmission error\n");
continue;
}
solve();
if(endd.size() == )
{
printf("Transmission error\n");
continue;
}
for (int i = ; i < endd.size(); i++)
endd[i] = qpow(g,endd[i],p);
sort(endd.begin(),endd.end());
for (int i = ; i < endd.size(); i++)
print(endd[i]);
} return ;
}

Hdu3223 Decrypt Messages的更多相关文章

  1. C#/PHP Compatible Encryption (AES256) ZZ

    Finding a way to encrypt messages in C# and decrypting them in PHP or vice versa seems to be a " ...

  2. RSA Encrypting/Decrypting、RSA+AES Encrypting/Decrypting

    catalogue . CryptoAPI介绍 . RSA Encrypting/Decrypting File 1. CryptoAPI介绍 0x1: Cryptography Service Pr ...

  3. sgu 142. Keyword 暴力,hash 难度:0

    142. Keyword time limit per test: 0.5 sec. memory limit per test: 16384 KB Kevin has invented a new ...

  4. SSH secure shell 原理与运用

    转: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 作者: 阮一峰 日期: 2011年12月21日 SSH是每一台Linux ...

  5. how to extract and decrypt WeChat EnMicromsg.db on Android phone

    One of my friend came to me with an Android phone. She saild somehting wrong with the hardware of he ...

  6. A very simple C++ module to encrypt/decrypt strings based on B64 and Vigenere ciper.

    A very simple C++ module to encrypt/decrypt strings based on B64 and Vigenere ciper. https://github. ...

  7. 微信破解,解密?How To Decrypt WeChat EnMicroMsg.db Database?

    20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...

  8. Error querying database. Cause: java.lang.IllegalArgumentException:Failed to decrypt.(错误笔记)

    java.lang.IllegalArgumentException:Failed to decrypt 从错误可以看出,解密失败. 原因是你在数据库连接配置的地方,设置了加密.即: config.d ...

  9. Kafka副本管理—— 为何去掉replica.lag.max.messages参数

    今天查看Kafka 0.10.0的官方文档,发现了这样一句话:Configuration parameter replica.lag.max.messages was removed. Partiti ...

随机推荐

  1. 后台程序获取JPG/GIF/PNG图片宽度、高度

    这是很久之前编写的代码,该代码是读取流数据指定位置的内容,获取图片的宽度.高度值. 由于系统更新,这些代码丢之不用,在这里存个档吧! 1. 获取gif图片宽度.高度.(binary_是图片流数据) ' ...

  2. ES6 之 解构赋值

    本博文配合 阮一峰 <ES6 标准入门(第3版)>一书进行简要概述 ES6 中变量的解构赋值. 数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这 ...

  3. SVN部署与简单使用

    原文发表于cu:2016-05-24 参考文档: http://www.tuicool.com/articles/Yv2iyu7 http://www.centoscn.com/CentosServe ...

  4. [C++] Solve "No source available for main()" error when debugging on Eclipse

    In Mac, the issue image: 1. A existing cmake project on disk 2. import this project into Eclipse. 3 ...

  5. Linux下使用vim编辑C程序

    这几天在系统能力班自学linux,加上最近大数据课上开始使用linux,我在这里总结一下,linux下使用vim编辑c程序的一些问题. 大数据课上是直接使用micro来编辑的,我这里只是简单的说明一下 ...

  6. Thunder团队第七周 - Scrum会议5

    Scrum会议5 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 宋雨沉迷于照相无法自拔,所以不在相片中. 参会成员: 王航:http://www.cnb ...

  7. Fisherman`Team的任务看板

     

  8. 四则运算——单元测试(测试方法:Right-BICEP )

    一.测试的具体部位 Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你是否可以强制错误条件发生? P-是否满足性能要 ...

  9. vue+vue-video-player实现弹窗播放视频

    将视频播放器标签放在对话框标签中,实现弹窗 template 中 <el-dialog :visible.sync="dialogVisible" width='680px' ...

  10. MySql 8 命令

    1-创建用户 create user 用户名@'%' identified by '密码'; create user 用户名@'localhost' identified by '密码';   2-授 ...