HDU--Secret Code

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1111

Problem 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

Source

Central
Europe 1999

题目大意:给你两个复数x,b(xr,xi,br,bi对应实数,虚数部分)求一个序列a满足 X = a0 + a1B + a2B^2 + ...+ anB^n

思路:这题是一直被坑啊,在我们学校一直wa,  怀疑数据问题跑到杭电上去刷(事实证明我想多了(。?_?。)?)

首先先来科普一下

秦九韶算法:

把一个n次多项式f(x)=a[n]x^n+a[n-1]x^(n-1)+......+a[1]x+a[0]

改写成如下形式:

f(x)=a[n]x^n+a[n-1]x^(n-1))+......+a[1]x+a[0]

=(a[n]x^(n-1)+a[n-1]x^(n-2)+......+a[1])x+a[0]

=((a[n]x^(n-2)+a[n-1]x^(n-3)+......+a[2])x+a[1])x+a[0]

=......  =(......((a[n]x+a[n-1])x+a[n-2])x+......+a[1])x+a[0].

求多项式的值时,首先计算最内层括号内一次多项式的值,即 v[1]=a[n]x+a[n-1]然后由内向外逐层计算一次多项式的值,即  v[2]=v[1]x+a[n-2]                v[3]=v[2]x+a[n-3]    ......   v[n]=v[n-1]x+a[0]这样,求n次多项式f(x)的值就转化为求n个一次多项式的值。(注:中括号里的数表示下标)上述方法称为秦九韶算法。直到今天,这种算法仍是多项式求值比较先进的算法

复数的除法::令t=c*c+d*d,(a+bi)/(c+di)=(ac+bd)/t+(bc-ad)/ti

然后深搜枚举a0~an,每次减去ai之后除以b后,剩下的就又有一个常数,直到0为止。除法的时候,由于要保证整除(就是上面的ac+bd和bc-ad是t的倍数),可以减少很多时间

代码如下

 1 #include <stdio.h>
 2 typedef __int64 LL;
 3 LL xr, xi, br, bi, sq, ans[101];
 4 int flag, k;
 5 //---------秦九韶算法----------
 6 void dfs(int step, LL real, LL com){
 7     LL tx, ty;
 8     if (step > 100 || flag) return;
 9     if (!real && !com){
10         flag = 1;
11         k = step;
12         return;
13     }
14     //复数除法 --t=c*c+d*d, (a+bi)/(c+di)=(ac+bd)/t+(bc-ad)/ti----
15     for (int i = 0; i*i < sq; i++){
16         tx = (real - i)*br + com*bi;
17         ty = -(real - i)*bi + com*br;
18         ans[step] = i;
19         if (!(tx%sq) && !(ty%sq))
20             dfs(step + 1, tx / sq, ty / sq);
21         if (flag) return;
22     }
23 }
24 int main(){
25     int i, t;
26     scanf("%d", &t);
27     while (t--){
28         flag = 0;
29         scanf("%I64d%I64d%I64d%I64d", &xr, &xi, &br, &bi);
30         sq = br*br + bi*bi;
31         dfs(0, xr, xi);
32         if (!flag)
33             printf("The code cannot be decrypted.\n");
34         else{
35             printf("%d", ans[k - 1]);
36             for (i = k - 2; i >= 0; i--)
37                 printf(",%I64d", ans[i]);
38             printf("\n");
39         }
40     }
41     return 0;
42 }

时间: 2024-10-12 15:41:26

HDU--Secret Code的相关文章

hdu 1111 Secret Code

http://acm.hdu.edu.cn/showproblem.php?pid=1111 复数除法: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int a[110]; 7 __int64 n; 8 int x1,y1,b1,b2; 9 int t1; 10 bool flag; 11 12 void dfs(int cnt) 1

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

Secret Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 670    Accepted Submission(s): 109 Problem Description The Sarcophagus itself is locked by a secret numerical code. When somebody wan

[swustoj 679] Secret Code

Secret Code 问题描述 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 cod

HDU 2017 Code Lock (并查集的应用+快速幂)

链接:HDU 3461 题目大意: 题目的大意是一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限次的"增加"操作后可以变成另一组密码,那么我们认为这两组密码是相同的.该题的目标就是在给定N.M和M个区间的前提下计算有多少种不同的密码. 根据题意,如果一个可调整的区间都没有的话,答案应该是26

HDU 3461 Code Lock(并查集的应用+快速幂)

* 65536kb,只能开到1.76*10^7大小的数组.而题目的N取到了10^7,我开始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 其实路径压缩也可以不用.............  题目的大意: 一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限

【微信】根据appid, secret, code获取用户基本信息

function getUserInfo(){ $appid = "yourappid"; $secret = "yoursecret"; $code = $_GET["code"]; $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&g

The secret code

The secret code Input file: stdinOutput file: stTime limit: 1 sec Memory limit: 256 MbAfter returning from the trip, Alex was unpleasantly surprised: his porch door had a new combination lock. Alexcan not get into his house! Code lock contains N disk

Android Secret Code

我们很多人应该都做过这样的操作,打开拨号键盘输入*#*#4636#*#*等字符就会弹出一个界面显示手机相关的一些信息,这个功能在Android中被称为android secret code,除了这些系统预置的secret code,我们也可以实现自己的secret code,而且实现起来非常简单. 要实现自己的secret code,只需要向系统注册一个Broadcast Receiver,不需要任何权限,如下所示: <receiver android:name=".SecretRecei

Android 编程下的 Secret Code

我们很多人应该都做过这样的操作,打开拨号键盘输入 *#*#4636#*#* 等字符就会弹出一个界面显示手机相关的一些信息,这个功能在 Android 中被称为 Android Secret Code,除了这些系统预置的 Secret Code,我们也可以实现自己的 Secret Code,而且实现起来非常简单. 要实现自己的 Secret Code,只需要向系统注册一个 Broadcast Receiver,不需要任何权限,如下所示: <receiver android:name=".Se

HDU 1111 Secret Code (DFS)

题目链接 题意 : 给你复数X的Xr和Xi,B的Br和Bi,让你求一个数列,使得X = a0 + a1B + a2B2 + ...+ anBn,X=Xr+i*Xi,B=Br+Bi*i : 思路 : 首先要知道复数的基本运算,题目中说0 <= ai < |B| ,|B|代表的是复数的模,|B|=√(Br*Br+Bi*Bi).将题目中给定的式子X = a0 + a1B + a2B2 + ...+ anBn,进行变型得:X=a0 + (a1 + (a2 + ...(an-1+ an*B))) .所以