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 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

 1 #include<stdio.h>
 2 #include<string.h>
 3 const int M = 5000000 ;
 4 typedef __int64 ll ;
 5 ll xr , xi , br , bi ;
 6 int n ;
 7 ll ini ;
 8 ll a[M] ;
 9 ll t ;
10
11 bool dfs (ll l , ll r , int dep)
12 {
13     if (dep > 100) return false ;
14     if (l == 0 && r == 0) {
15         n = dep ;
16         return true ;
17     }
18     ll al , ar ;
19     for (int i = 0 ; i * i < ini ; i ++) {
20         al = l - i ; ar = r ;
21         if ( ( (1ll * al * br + 1ll *ar * bi) % t ) == 0 && ((1ll *ar * br -1ll * al * bi) % t) == 0 ) {
22             a[dep] = i ;
23             if ( dfs ( ((1ll * al * br + 1ll * ar * bi) / t) , ((1ll * ar * br - 1ll * al * bi) / t) , dep + 1) )
24                 return true ;
25         }
26     }
27     return false ;
28 }
29
30 int main ()
31 {
32     //freopen ("a.txt" , "r" , stdin ) ;
33     ll T ;
34     scanf ("%I64d" , &T ) ;
35     while (T --) {
36         scanf ("%I64d%I64d%I64d%I64d" , &xr , &xi , &br , &bi ) ;
37         t = br * br + bi * bi ;
38         ini = br * br + bi * bi ;
39         if (dfs (xr  , xi , 0) ) {
40              if (n == 0) puts ("0") ;
41              else {
42                 for (int i = n - 1 ; i >= 0 ; i --) printf ("%I64d%c" , a[i] , i == 0 ? ‘\n‘ : ‘,‘) ;
43              }
44         }
45         else puts ("The code cannot be decrypted.") ;
46     }
47     return 0 ;
48 }

秦九韶算法:

一般地,一元n次多项式的求值需要经过[n(n+1)]/2次乘法和n次加法,而秦九韶算法只需要n次乘法和n次加法。在人工计算时,一次大大简化了运算过程。

把一个n次多项式

改写成如下形式:

多项式的值时,首先计算最内层括号内一次多项式的值,即

然后由内向外逐层计算一次多项式的值,即

这样,求n次多项式f(x)的值就转化为求n个一次多项式的值。

结论:对于一个n次多项式,至多做n次乘法和n次加法。(当最高次项系数不为1时分别为n次乘法和n次加法 ,当最高次项系数为1时,分别为n-1 次乘法 ,n次加法。)

复数除法运算:

设复数 a + bi  ,  c + di ;

t = c * c + d * d ;

则 (a + bi) / (c + di ) = (ac + bd) / t + (bc - ad) / t  * i ;

时间: 2025-01-11 10:25:48

hdu.1111.Secret Code(dfs + 秦九韶算法)的相关文章

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))) .所以

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

[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 Hotaru&#39;s problem(Manacher算法+贪心)

manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成,第一段和第二段对称,第一段和第三段一样. 思路:首先利用Manacher算法求出以任意两个相邻元素为中心的回文串长度,用a[i]表示i-1,i为中心的回文串长度的一半, 那么问题就转化成了求最大的x,使得a[i]>=x,a[i+x]>=x,这一步可以贪心来做. 将a[i]从大到小排序(间接排序保留

hdu 3572 Task Schedule(网络流 dinic算法)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1197 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

秦九韶算法求解多项式

秦九韶算法是中国南宋时期的数学家秦九韶提出的一种多项式简化算法.在西方被称作霍纳算法.它是一种将一元n次多项式的求值问题转化为n个一次式的算法. 一般地,一元n次多项式的求值需要经过[n(n+1)]/2次乘法和n次加法,而秦九韶算法只需要n次乘法和n次加法.其大大简化了计算过程,即使在现代,利用计算机解决多项式的求值问题时,秦九韶算法依然是最优的算法. 题目:写程序计算给定多项式在给定点x处的值 f(x) = a0 + a1x + … + an-1xn-1 + anxn 分析:对比使用常规算法和

hdu 5375 Gray code(DP)

hdu 5375 Gray code Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed

算法 秦九韶算法

秦九韶算法又叫霍纳算法. 一般地,一元n次多项式的求值需要经过[n(n+1)]/2次乘法和n次加法,而秦九韶算法只需要n次乘法和n次加法.在人工计算时,一次大大简化了运算过程. Pn(x)= anx ^n+a(n-1)x^(n-1)+…+a1x+a0 可简化成 Pn(x)= anx ^n+a(n-1)x^(n-1)+…+a1x+a0=((…(((anx +an-1)x+an-2)x+ an-3)…)x+a1)x+a0 1 package com.qyf404.lean.algorithm; 2

bzoj3157国王奇遇记(秦九韶算法+矩乘)

bz第233题,用一种233333333的做法过掉了(为啥我YY出一个算法来就是全网最慢的啊...) 题意:求sigma{(i^m)*(m^i),1<=i<=n},n<=10^9,m<=200 别人的做法: O(m^2logn),O(m^2),甚至O(m)的神做法 学渣的做法:矩乘+秦九韶算法,O(m^3logn),刚好可以过最弱版本的国王奇遇记的数据 (极限数据单点其实是1.2s+,不想继续卡常了-bzoj卡总时限使人懒惰-如果把矩乘的封装拆掉可能会快点吧,然而人弱懒得拆了...