hdu 1111 Secret Code(复数·展开·深搜·输出)

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

Secret Code

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


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
分析:读了半天题。。。大意:Xr+iXi=a0+a1B+a2B^2+……+anB^n,其中B=Br+iBi (复数) 将其展开:Xr+iXi=a0+[(((a1+a2B)+a3B^2)+a4B^3)……+anB^(n-1)]B 接着,
等式左右两边减去a0,然后除以B;
等式左右两边减去a1,然后除以B;
……
直到最后减去an刚好等于0.
由于ai是未知的,且0<=ai<|B|,|Bi|<=16所以可以使用深搜。n<=100注意利用它及时退出,不然会TLE。关于输出的问题:逆序输出要么利用数组要么利用结构体,当然这里不是逆序输出。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
LL t,xr,xi,br,bi,by;
LL mother,ans[110],top;
bool flag=0;
void dfs(LL a){ 
    if(flag) return ;
    LL q1=(xr-a)*br+xi*bi,q2=xi*br-bi*(xr-a);
    if(top>100 || q1%mother!=0 || q2%mother!=0){ return ; }
    q1=q1/mother;   q2=q2/mother;
    xr=q1;   xi=q2;
    ans[top++]=a;
    if(xr==0&&xi==0){ flag=1; return; }
    for(LL i=0;i<by;i++) dfs(i);
}
int main()
{
    //freopen("cin.txt","r",stdin);
    cin>>t;
    while(t--){
        scanf("%lld%lld%lld%lld",&xr,&xi,&br,&bi);
        flag=0;
        mother=br*br+bi*bi;
        by=(LL)sqrt(mother)+1;
        for(LL i=0;i<by;i++){
            if(flag) break;
            top=0;
            dfs(i);
        }
        if(flag){
            for(int i=top-1;i>0;i--){
                printf("%lld,",ans[i]);
            }
            printf("%lld\n",ans[0]);
        }
        else {
            puts("The code cannot be decrypted.");
        }
    }
    return 0;
}



你可能感兴趣的:(number,Complex,HDU)