HDU 1111 Secret Code(数论的dfs)
Description
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
Output
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
-935 2475 -11 -15
1 0 -3 -2
93 16 3 2
191 -192 11 -12
Sample Output
1
The code cannot be decrypted.
16,15
|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)的更多相关文章
- hdu.1111.Secret Code(dfs + 秦九韶算法)
Secret Code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU 1111 Secret Code (DFS)
题目链接 题意 : 给你复数X的Xr和Xi,B的Br和Bi,让你求一个数列,使得X = a0 + a1B + a2B2 + ...+ anBn,X=Xr+i*Xi,B=Br+Bi*i : 思路 : 首 ...
- hdu 1111 Secret Code
http://acm.hdu.edu.cn/showproblem.php?pid=1111 复数除法: #include <cstdio> #include <cstring> ...
- [swustoj 679] Secret Code
Secret Code 问题描述 The Sarcophagus itself is locked by a secret numerical code. When somebody wants to ...
- Android Secret Code
我们很多人应该都做过这样的操作,打开拨号键盘输入*#*#4636#*#*等字符就会弹出一个界面显示手机相关的一些信息,这个功能在Android中被称为android secret code,除了这些系 ...
- Android 编程下的 Secret Code
我们很多人应该都做过这样的操作,打开拨号键盘输入 *#*#4636#*#* 等字符就会弹出一个界面显示手机相关的一些信息,这个功能在 Android 中被称为 Android Secret Code, ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- The secret code
The secret code Input file: stdinOutput file: stTime limit: 1 sec Memory limit: 256 MbAfter returnin ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
随机推荐
- 关于jqgrid的一些使用
1.jqgrid如何切换中英文 在做电力监控系统的时候,根据项目的需要涉及到中英文的切换,一直纠结了好久没有好的办法,虽然我知道可以手动更改引入的js文件就可以更改中英文,但是动态的一直没有办法更改, ...
- pL/SQL 创建DBLIKN
某些时候,需要关联不同的数据库进行数据查询.操作等. 在Oracle中,关联不同的数据库进行表关联,就要用到了数据库连接(DB link). 创建DB link有两种方法:通过SQL语句创建,通过可视 ...
- SpringSecurity 3.2入门(5)自定义登录页面
增加spring-security.xml文件配置如下 <!-- 配置SpringSecurity的http安全服务 --> <security:http auto-config=& ...
- OGNL,表达式上下文ContextMap
1.OGNL表达式 object graph navigation language:对象图导航语言 存取对象属性:调用对象方法:字段类型转换等. <input type="text& ...
- vs.net打包(自动检测环境并安装.net framwork)
vs.net打包程序或者制作安装程序时自动检测环境并安装.netframwork的设置方法之前我看过文档也做过,但是过一段时间又忘了,现在终于又找到方法了,还是把这个方法写下来吧,方便自己也方便大家将 ...
- App调用safar
/调用safar打开网页 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.cnblo ...
- 【学习笔记】使用SQLyog连接MySQL数据库
一.使用SQLyog创建数据库用来管理学生信息 #创建数据库student DROP DATABASE IF EXISTS Myschool; CREATE DATABASE Myschool; #在 ...
- Java循环结构之while和do-while循环
循环结构知识点 本章技能目标 1理解循环的含义 2会使用while循环 3会使用do-while循环 4会使用调试解决简单的程序错误 一. 循环的含义 循环就是重复的做……(Java程序中的循环结 ...
- js之箭头函数
原文 ES6标准新增了一种新的函数:Arrow Function(箭头函数). 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: ...
- Flask插件---flask_script与flask_migrate
import app from flask_script import Manager from flask_migrate import Migrate,MigrateCommand my_app ...