Secret Code

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

The Sarcophagus itself is locked by a secret numerical code. When somebody wants to open it, he must know the code and set it exactly on the top of the Sarcophagus. A very intricate mechanism then opens the cover. If an incorrect code is entered, the tickets inside would catch fire immediately and they would have been lost forever. The code (consisting of up to 100 integers) was hidden in the Alexandrian Library but unfortunately, as you probably know, the library burned down completely.

But an almost unknown archaeologist has obtained a copy of the code something during the 18th century. He was afraid that the code could get to the ``wrong people'' so he has encoded the numbers in a very special way. He took a random complex number B that was greater (in absolute value) than any of the encoded numbers. Then he counted the numbers as the digits of the system with basis B. That means the sequence of numbers an, an-1, ..., a1, a0 was encoded as the number X = a0 + a1B + a2B2 + ...+ anBn.

Your goal is to decrypt the secret code, i.e. to express a given number X in the number system to the base B. In other words, given the numbers X and Byou are to determine the ``digit'' a0 through an.

 

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case consists of one single line containing four integer numbers Xr, Xi, Br, Bi (|Xr|,|Xi| <= 1000000, |Br|,|Bi| <= 16). These numbers indicate the real and complex components of numbers X and B, i.e. X = Xr + i.Xi, B = Br + i.Bi. B is the basis of the system (|B| > 1), X is the number you have to express.
 

Output

Your program must output a single line for each test case. The line should contain the ``digits'' an, an-1, ..., a1, a0, separated by commas. The following conditions must be satisfied:

for all i in {0, 1, 2, ...n}: 0 <= ai < |B|

X = a0 + a1B + a2B2 + ...+ anBn

if n > 0 then an <> 0

n <= 100

If there are no numbers meeting these criteria, output the sentence "The code cannot be decrypted.". If there are more possibilities, print any of them.
 

Sample Input

4
-935 2475 -11 -15
1 0 -3 -2
93 16 3 2
191 -192 11 -12
 

Sample Output

8,11,18
1
The code cannot be decrypted.
16,15
 
题目意思:
输入一个复数X=xr+xi以及复数B=br+bi,求数列a[n]使得x=a0+a1*b+a2*b^2+…an*b^n。其中|Xi| <= 1000000, |Bi| <= 16,n<=100,
|b|>ai>=0,|b|>1.
 
分析:
参考了一下别人的思路:
  • 暴力搜索,把要搜索的序列{an}想成一个森林,a0在第一层,a1在下一层,以此类推
  • 秦九韶算法:x=a0+(a1+(a2+(a3+…)*b)*b)*b,容易想到递归实现
  • 为了方便运算,把实部、虚部拆开一起递归,每次递归枚举下一层a(i+1)的所有可能结果,很容易想到{an}必定都为整数序列,每枚举一个a(i+1),判断[X-a(i)]%b能否整除(实部与实部、虚部与虚部),满足继续dfs,不满足继续枚举,直到Xr==0,Xi==0递归结束,当然不能超过100层。最后得到的序列逆序输出即可

看了大佬的思路才懂。。。。

code:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<memory.h>
using namespace std;
int xr,xi,br,bi,con;
int flag,t;
int a[];
void dfs(int n)
{
int x,y,i;
if(n>)//多项式最多00项
return ;
if(xr==&&xi==)
{
flag=;
t=n;
return ;
}
for(i=;i*i<con;i++)
{
//xi减去a[i]后剩下的部分,根据复数的除法运算可以得到如下表达式 x=x+yi;
x=(xr-i)*br+xi*bi;
y=xi*br-(xr-i)*bi;
a[n]=i;
if(x%con==&&y%con==)//保证整除
{
xr=x/con;
xi=y/con;
dfs(n+);
}
if(flag)
return ;
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>xr>>xi>>br>>bi;
con=br*br+bi*bi;
flag=;
dfs();
if(!flag)
{
cout<<"The code cannot be decrypted."<<endl;
}else
{
cout<<a[t-];
for(int i=t-;i>=;i--)
{
cout<<','<<a[i];
}
cout<<endl;
}
}
return ;
}

HDU 1111 Secret Code(数论的dfs)的更多相关文章

  1. hdu.1111.Secret Code(dfs + 秦九韶算法)

    Secret Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. HDU 1111 Secret Code (DFS)

    题目链接 题意 : 给你复数X的Xr和Xi,B的Br和Bi,让你求一个数列,使得X = a0 + a1B + a2B2 + ...+ anBn,X=Xr+i*Xi,B=Br+Bi*i : 思路 : 首 ...

  3. hdu 1111 Secret Code

    http://acm.hdu.edu.cn/showproblem.php?pid=1111 复数除法: #include <cstdio> #include <cstring> ...

  4. [swustoj 679] Secret Code

    Secret Code 问题描述 The Sarcophagus itself is locked by a secret numerical code. When somebody wants to ...

  5. Android Secret Code

    我们很多人应该都做过这样的操作,打开拨号键盘输入*#*#4636#*#*等字符就会弹出一个界面显示手机相关的一些信息,这个功能在Android中被称为android secret code,除了这些系 ...

  6. Android 编程下的 Secret Code

    我们很多人应该都做过这样的操作,打开拨号键盘输入 *#*#4636#*#* 等字符就会弹出一个界面显示手机相关的一些信息,这个功能在 Android 中被称为 Android Secret Code, ...

  7. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  8. The secret code

    The secret code Input file: stdinOutput file: stTime limit: 1 sec Memory limit: 256 MbAfter returnin ...

  9. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

随机推荐

  1. 【程序员技术练级】学习一门脚本语言 python(一)文件处理

    现在工作上主要用的语言是java,java在企业级的应用上能够发挥很好的用途,但有时候要做一个小功能时,比如批量更新文件,抓取网页等,这时候用java就显得太笨重了.因此就学习了python这门脚本语 ...

  2. C#(Winform)的SaveFileDialog(文件保存对话框)控件使用

       #region 保存对话框   private void ShowSaveFileDialog()   {         //string localFilePath, fileNameExt ...

  3. React.js 小书 Lesson9 - 事件监听

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson9 转载请注明出处,保留原文链接和作者信息. 在 React.js 里面监听事件是很容易的事情 ...

  4. SQL 脚本整理 笔记

    1.视图 存储过程 触发器 批量加密(With Encryption),单个解密 在运行过程中自己找不到启用DAC 的地方,链接的时候需要在服务器名称前面添加ADMIN:,如本机是ADMIN:WP-P ...

  5. 《C#高效编程》读书笔记02-用运行时常量(readonly)而不是编译期常量(const)

    C#有两种类型的常量:编译期常量和运行时常量.两者有截然不同的行为,使用不当的话,会造成性能问题,如果没法确定,则使用慢点,但能保证正确的运行时常量. 运行时常量使用readonly关键字声明,编译期 ...

  6. Razor语句(VIew)小知识

    1.可以写后台语句 例如:

  7. oracle 常用操作记录--持续更新...

    一.oracle grant 授权语句(转自:https://www.cnblogs.com/yt954437595/p/6488819.html) --select * from dba_users ...

  8. SpringBoot ------ 使用AOP处理请求

    一.AOP统一处理请求日志 1.spring的两大核心:AOP ,  IOC 2.面向对象OOP关注的是将需求功能垂直,划分为不同的,并且相对独立的,   会封装成良好的类,并且类有属于自己的行为. ...

  9. javaSystem.out.println()输出byte[]和char[]异常的问题

    javaSystem.out.println()输出byte[]和char[]异常的问题 今天 突然有人问我他写的byte[]和char[],在用System.out.println()输出的时候所得 ...

  10. The nineteenth day

    Twinkle,twinkle,little start! 闪烁,闪烁,小星星 How I wonder what you are, 我想知道你是什么 Up above the world so hi ...