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. vue2.0做移动端开发用到的相关插件和经验总结

    最近一直在做移动端微信公众号项目的开发,也是我首次用vue来开发移动端项目,前期积累的移动端开发经验较少.经过这个项目的锻炼,加深了对vue相关知识点的理解和运用,同时,在项目中所涉及到的微信api( ...

  2. windows下sublime text的node.js开发环境搭建

    首先安装sublime text3,百度一堆,自己找吧.理论上sublime text2应该也可以.我只能说一句:这个软件实在是太强悍了. 跨平台,丰富的插件体系,加上插件基本上就是一个强悍的ide了 ...

  3. 239. [LeetCode ]Sliding Window Maximum

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  4. Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation

    一.错误信息 Entity Framework 6.0数据迁移:Add-Migration XXXX 命令发生错误 System.Reflection.TargetInvocationExceptio ...

  5. HTML5+Bootstrap 学习笔记 3

    HTML5 aria-* and role aria是指Accessible Rich Internet Application.role的作用是描述一个非标准的tag的实际作用,而aria-*的作用 ...

  6. Python基础知识-05-数据类型总结字典

    python其他知识目录 1.一道题,选择商品的序号.程序员和用户各自面对的序号起始值 如有变量 googs = ['汽车','飞机','火箭'] 提示用户可供选择的商品: 0,汽车1,飞机2,火箭用 ...

  7. Spark SQL、DataFrame和Dataset——转载

    转载自:  Spark SQL.DataFrame和Datase

  8. 谈谈javascript中的变量提升还有函数提升

    在很多面试题中,经常会看到关于变量提升,还有函数提升的题目,所以我就写一篇自己理解之后的随笔,方便之后的查阅和复习. 首先举个例子 foo();//undefined function foo(){ ...

  9. C++ STL 优先队列 priority_queue 详解(转)

    转自https://blog.csdn.net/c20182030/article/details/70757660,感谢大佬. 优先队列 引入 优先队列是一种特殊的队列,在学习堆排序的时候就有所了解 ...

  10. 四则运算4 WEB(结对开发)

    在第三次实验的基础上,teacher又对此提出了新的要求,实现网页版或安卓的四则运算. 结对开发的伙伴: 博客名:Mr.缪 姓名:缪金敏 链接:http://www.cnblogs.com/miaoj ...