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.
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
秦九昭,DFS
X=a0+a1*B+a2*B^2+…an*B^n => X=a0+(a1+(a2+…)*B)*B)*B
处理 :减ai后,除B,直到0为止
复数的模 |Z|=|a+bi|=sqrt(a*a+b*b) , 除法 (a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d) (分子分母同乘(c-di))
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll xr,xi,br,bi,mu;
int num_a,t;
int path[105];
bool flag=0;
void dfs(ll xr,ll xi,int num_a)
{
ll xrr,xii;
if(num_a>100||flag)
return ;
if(xr==0&&xi==0)
{
t=num_a;
flag=1;
return ;
}
for(int i=0;i*i<mu;i++)
{
if(flag)
return;
xrr=xr,xii=xi;
xrr-=i;
if((xrr*br+xii*bi)%mu!=0||(xii*br-xrr*bi)%mu!=0)
continue;
ll qxrr=xrr;
xrr=(xrr*br+xii*bi)/mu;
xii=(xii*br-qxrr*bi)/mu;
path[num_a]=i;
dfs(xrr,xii,num_a+1);
}
}
int main()
{
int kase;
cin>>kase;
while(kase--)
{
cin>>xr>>xi>>br>>bi;
mu=br*br+bi*bi;
flag=0;
dfs(xr,xi,0);
if(!flag)
{
cout<<"The code cannot be decrypted."<<endl;
continue;
}
cout<<path[t-1];
for(int i=t-2;i>=0;i--)
cout<<','<<path[i];
cout<<endl;
}
return 0;
}