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. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  2. Zookeeper--java操作zookeeper

    如果是使用java操作zookeeper,zookeeper的javaclient 使我们更轻松的去对zookeeper进行各种操作,我们引入zookeeper-3.4.5.jar 和 zkclien ...

  3. Visual Assist 试用期过期怎么办?

    Visual Assist 试用期过期怎么办 VS这个强大的编译器常常会配置番茄小助手 Visual Assist,但是有时候试用期会过期,又想免费试用,怎么办呢? 有一个方法可以充值番茄助手的试用期 ...

  4. centos 系统初始化

    centos 系统初始化 #!/bin/bash # author cfwl create date of 2012-10-21 # blog http://cfwlxf.blog.51cto.com ...

  5. (转)一篇写的简明易懂的logging模块

    转:http://kenby.iteye.com/blog/1162698 一.从一个使用场景开始 开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件 import logging # 创 ...

  6. apply新用法,最大值查找

    要找到数组中的最大或最小值,可以像下面这样使用apply() var values=[1,2,3,4,5,6,7,8]; var max = Math.max.apply(Math,values); ...

  7. 关于JavaScript定时器我的一些小理解

    因为自己在平时工作中,有些功能需要用到定时器,但是定时器并不像我们表边上看到的那样,所以这周末我看看书查查资料,深入研究了一下JavaScript中的定时器,那么废话不多说,下面进入我们今天的正题. ...

  8. 如何理解IPD+CMMI+Scrum一体化研发管理解决方案之IPD篇

    如何快速响应市场的变化,如何推出更有竞争力的产品,如何在竞争中脱颖而出,是国内研发企业普遍面临的核心问题,为了解决这些问题,越来越多的企业开始重视创新与研发管理,加强研发过程的规范化,集成产品开发(I ...

  9. Scrum立会报告+燃尽图(十月十日总第一次):选题

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2190 Scrum立会master:付佳 一.小组介绍 组长:付佳 组员: ...

  10. "感应锁屏"Alpha版使用说明

    “感应锁屏”Alpha版使用说明 一.产品介绍 感应锁屏是锁屏软件的一大创新.相比传统的锁屏软件,“感应锁屏”从可操作性.方便性.功能全面性都有了很大的提升,可以让用户方便快捷的进行锁屏操作. “感应 ...