秦九韶算法—— HDU 1111 Secret Code

对应HDU题目:点击打开链接

Secret Code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 748    Accepted Submission(s): 133


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
 

题意:

给出两个复数X = a + bi;B = c + di。其中X与B的关系为:

X = a0 + a1*B + a2 * B^2 + a3 * B^3 + ... + an * B^n

且满足 0 <= ai < |B|  (0 <= i <= n)  且n <= 100;求任意一组{a0, a1, a2, a3, ... , an} 并反向输出。如果没有就按要求输出。


思路:

首先是|B| 表示复数的模;如B = c + di,则|B| = sqrt(c * c + d * d);

X = a0 + a1*B + a2 * B^2 + a3 * B^3 + ... + an * B^n

   = a0 + B*(a1 + B*(a2 + B*(a3 + ... B*(a_n-1 + B*an)...)))

写成秦九韶形式就枚举ai了,每次用(X - ai) / B 直到 X的实部与虚部都为0或者n > 100。注意X = 0 + 0i 特例。。。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define N 105
typedef long long LL;
int br, bi, k;

bool Dfs(LL xr, LL xi, LL *a, int &cnt)
{
    if(cnt > 100) return false;
    if(0 == xr && 0 == xi) return true;
    int i;
    LL n_xr, n_xi;
    for(i = 0; i * i < k; i++){
        a[cnt++] = i;
        n_xr = (xr - i) * br + xi * bi;
        n_xi = xi * br - (xr - i) * bi;
        if(n_xr % k == 0 && n_xi % k == 0)
            if(Dfs(n_xr / k, n_xi / k, a, cnt) == true) return true;
        cnt--;
    }
    return false;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int T;
    LL a[N];
    scanf("%d", &T);
    while(T--){
        int xr, xi;
        int i, cnt = 0;
        scanf("%d%d%d%d", &xr, &xi, &br, &bi);
		if(0 == xr && 0 == xi){
			printf("0\n");
			continue;
		}
        k = br * br + bi * bi;
        bool ok = Dfs((LL)xr, (LL)xi, a, cnt);
        if(ok == false) printf("The code cannot be decrypted.\n");
        else{
            for(i = cnt - 1; i >= 0; i--){
                printf("%I64d", a[i]);
                if(i) printf(",");
            }
            printf("\n");
        }
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值