1
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
using namespace std;
typedef long long LL;
LL a,m;
LL p,q,r,s;
char str[40],str_save[40];
LL num[40],A[40],num_save[40],A_temp[40];
int tot_save;
void Refresh(int t)
{
int tot=1;
str[1]='A';
num[1]=0;
for(int i=t;i>=0;i--)
{
if(A[i]!=0)
{
if(str[tot]=='A')
num[tot]=A[i];
else
{
if(num[tot]!=0)
tot++;
str[tot]='A';
num[tot]=A[i];
}
}
if(i>0)
{
if(str[tot]=='M')
num[tot]++;
else
{
if(num[tot]!=0)
tot++;
str[tot]='M';
num[tot]=1;
}
}
}
while(num[tot]==0&&tot>0)
tot--;
for(int i=1;i<=tot;i++)
if(str[i]=='A')
num[i]/=a;
bool re=false;
if(str_save[1]='@')
re=true;
else
{
LL cnt0=0,cnt1=0;
for(int i=1;i<=tot_save;i++)
cnt0+=num_save[i];
for(int i=1;i<=tot;i++)
cnt1+=num[i];
if(cnt1>cnt0)
return;
if(cnt1<cnt0)
re=true;
else
{
for(int d0=1,d1=1;;)
{
if(d0>tot_save)
return;
if(str_save[d0]<str[d1])
return;
if(str_save[d0]>str[d1])
{
re=true;
break;
}
if(num_save[d0]==num[d1])
{
d0++;
d1++;
continue;
}
if(str_save[d0]=='A'&&num_save[d0]>num[d1])
return;
if(str_save[d0]=='M'&&num_save[d0]<num[d1])
return;
re=true;
break;
}
}
}
if(re)
{
tot_save=tot;
for(int i=1;i<=tot;i++)
{
str_save[i]=str[i];
num_save[i]=num[i];
}
}
}
void Output()
{
if(str_save[1]=='@')
{
cout<<" impossible\n";
return;
}
for(int i=1;i<=tot_save;i++)
cout<<" "<<num_save[i]<<str_save[i];
cout<<"\n";
}
void Solve()
{
if(p>=r&&p<=s)
{
cout<<" empty\n";
return;
}
if(m==1)
{
int dtemp=(r-p)/a;
while(dtemp*a+p<r)
dtemp++;
if(q+dtemp*a+q<=s)
cout<<dtemp<<"A"<<"\n";
else
cout<<" impossible\n";
return;
}
LL maxk=1,maxt=0;
for(;maxk*p<=s;maxk*=m,maxt++);
maxk/=m;maxt--;
str_save[1]='@';
for(LL t=maxt,k=maxk;t>=0;k/=m,t--)
{
for(int i=0;i<=t;i++)
A[i]=0;
A[t]=p;
LL sum=p*k;
for(int i=t,k0=k;i>=0;i--,k0/=m)
{
if((sum+k0*a)<=r)
{
LL temp=(r-sum)/(k0*a);
sum+=temp*k0*a;
A[i]+=temp*a;
}
}
sum=0;
for(int i=t,k0=k;i>=0;i--,k0/=m)
{
sum+=A[i]*k0;
if(sum>=r && (sum-p*k + q*k)<=s)
{
for(int j=t;j>=0;j--)
A_temp[j]=A[j];
A[t]-=p;
for(int j=i-1;j>=0;j--)
A[j]=0;
Refresh(t);
for(int j=t;j>=0;j--)
A[j]=A_temp[j];
}
if((sum+k0*a)>=r&&(sum+k0*a-p*k+q*k)<=s)
{
for(int j=t;j>=0;j--)
A_temp[j]=A[j];
A[t]-=p;
A[i]+=a;
for(int j=i-1;j>=0;j--)
A[j]=0;
Refresh(t);
for(int j=t;j>=0;j--)
A[j]=A_temp[j];
}
}
}
Output();
}
int main()
{
int Case=0;
while(cin>>a>>m>>p>>q>>r>>s && (a||m||p||q||r||s))
{
cout<<"Case "<<++Case<<":";
Solve();
}
return 0;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<math.h>
#include<vector>
using namespace std;
#define MP make_pair
#define PB push_back
#define key1 first.first
#define key2 first.second
#define key3 second.first
#define key4 second.second
int abs(int x)
{
return x<0?-x:x;
}
struct Matrix
{
double v[3][3];
}RotMatrix;
struct Vector
{
double X,Y;
}pf[4],_p[4];
struct Vector_int
{
int X,Y;
}p[4],q[4];
int ax,ay;
vector<pair<pair<int,int>, pair<int,int>>> Trans;
bool used[10000],line;
Vector operator * (Vector A,Matrix B)
{
double xx=A.X*B.v[1][1] + A.Y*B.v[1][2];
double yy=A.X*B.v[2][1] + A.Y*B.v[2][2];
Vector Ret;
Ret.X==xx;Ret.Y==yy;return Ret;
}
int snap(double num)
{
if(num>=0)
return (int)(num+0.5);
else
return -snap(0.0-num);
}
void Check(int _id1,int _id2,int _id3,int Rot_x,int Rot_y)
{
pf[_id1]=_p[1];pf[_id2]=_p[2];pf[_id3]=_p[3];
double costh=Rot_x/sqrt(double(Rot_x*Rot_x+Rot_y*Rot_y));
double sinth=Rot_y/sqrt(double(Rot_x*Rot_x+Rot_y*Rot_y));
RotMatrix.v[1][1]=costh;
RotMatrix.v[1][2]=-sinth;
RotMatrix.v[2][1]=sinth;
RotMatrix.v[2][2]=costh;
pf[1]=pf[1]*RotMatrix;
pf[2]=pf[2]*RotMatrix;
pf[3]=pf[3]*RotMatrix;
p[1].X=snap(pf[1].X);p[1].Y=snap(pf[1].Y);
p[2].X=snap(pf[2].X);p[2].Y=snap(pf[2].Y);
p[3].X=snap(pf[3].X);p[3].Y=snap(pf[3].Y);
ax=0;
if(q[1].X-q[2].X!=0&&p[1].X-p[2].X!=0)
{
if(abs(q[1].X-q[2].X)%abs(p[1].X-p[2].X)==0)
{
if(ax==0)
ax=(q[1].X-q[2].X)/(p[1].X-p[2].X);
else if((q[1].X-q[2].X)/(p[1].X-p[2].X)!=ax)
return;
}
else
return;
}
else if(q[1].X-q[2].X!=0||p[1].X-p[2].X!=0)
return;
if(q[1].X-q[3].X!=0&&p[1].X-p[3].X!=0)
{
if(abs(q[1].X-q[3].X)%abs(p[1].X-p[3].X)==0)
{
if(ax==0)
ax=(q[1].X-q[3].X)/(p[1].X-p[3].X);
else if((q[1].X-q[3].X)/(p[1].X-p[3].X)!=ax)
return;
}
else
return;
}
else if(q[1].X-q[3].X!=0||p[1].X-p[3].X!=0)
return;
if(q[2].X-q[3].X!=0&&p[2].X-p[3].X!=0)
{
if(abs(q[2].X-q[3].X)%abs(p[2].X-p[3].X)==0)
{
if(ax==0)
ax=(q[2].X-q[3].X)/(p[2].X-p[3].X);
else if((q[2].X-q[3].X)/(p[2].X-p[3].X)!=ax)
return;
}
else
return;
}
else if(q[2].X-q[3].X!=0||p[2].X-p[3].X!=0)
return;
ay=0;
if(q[1].Y-q[2].Y!=0&&p[1].Y-p[2].Y!=0)
{
if(abs(q[1].Y-q[2].Y)%abs(p[1].Y-p[2].Y)==0)
{
if(ay==0)
ay=(q[1].Y-q[2].Y)/(p[1].Y-p[2].Y);
else if((q[1].Y-q[2].Y)/(p[1].Y-p[2].Y)!=ay)
return;
}
else
return;
}
else if(q[1].Y-q[2].Y!=0||p[1].Y-p[2].Y!=0)
return;
if(q[1].Y-q[3].Y!=0&&p[1].Y-p[3].Y!=0)
{
if(abs(q[1].Y-q[3].Y)%abs(p[1].Y-p[3].Y)==0)
{
if(ay==0)
ay=(q[1].Y-q[3].Y)/(p[1].Y-p[3].Y);
else if((q[1].Y-q[3].Y)/(p[1].Y-p[3].Y)!=ay)
return;
}
else
return;
}
else if(q[1].Y-q[3].Y!=0||p[1].Y-p[3].Y!=0)
return;
if(q[2].Y-q[3].Y!=0&&p[2].Y-p[3].Y!=0)
{
if(abs(q[2].Y-q[3].Y)%abs(p[2].Y-p[3].Y)==0)
{
if(ay==0)
ay=(q[2].Y-q[3].Y)/(p[2].Y-p[3].Y);
else if((q[2].Y-q[3].Y)/(p[2].Y-p[3].Y)!=ay)
return;
}
else
return;
}
else if(q[2].Y-q[3].Y!=0||p[2].Y-p[3].Y!=0)
return;
if(ax==0||ay==0)
line=true;
Trans.PB(MP(MP(Rot_x,Rot_y),MP(ax,ay)));
}
void Affine(int _id1,int _id2,int _id3)
{
for(int x=-9;x<=9;x++)
{
Check(_id1,_id2,_id3,x,+10);
Check(_id1,_id2,_id3,x,-10);
}
for(int y=-10;y<=10;y++)
{
Check(_id1,_id2,_id3,+10,y);
Check(_id1,_id2,_id3,-10,y);
}
}
void Solve()
{
line=false;
Trans.clear();
Affine(1,2,3);
Affine(1,3,2);
Affine(2,1,3);
Affine(2,3,1);
Affine(3,1,2);
Affine(3,2,1);
if(Trans.size()==0)
{
cout<<"no solution\n";
return;
}
int ans=0;
for(int i=0;i<Trans.size();i++)
used[i]=false;
for(int i=0;i<Trans.size();i++)
if(!used[i])
{
ans++;
for(int j=i+1;j<Trans.size();j++)
{
if(Trans[i].key1 + Trans[j].key1==0&&
Trans[i].key2 + Trans[j].key2==0&&
Trans[i].key3 + Trans[j].key3==0&&
Trans[i].key4 + Trans[j].key4==0)
used[j]=true;
}
}
if(ans==1&&!line)
cout<<"equivalent solutions\n";
else
cout<<"inconsistent solutions\n";
}
int main()
{
int Case=0;
while(cin>>_p[1].X>>_p[1].Y>>_p[2].X>>_p[2].Y>>_p[3].X>>_p[3].Y &&(_p[1].X||_p[1].Y||_p[2].X||_p[2].Y||_p[3].X||_p[3].Y))
{
cin>>q[1].X>>q[1].Y>>q[2].X>>q[2].Y>>q[3].X>>q[3].Y;
cout<<"Case "<<++Case<<": ";
Solve();
}
return 0;
}
3
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<string>
#include<stdlib.h>
using namespace std;
#define PB push_back
int N,M,A[209][209];
bool vis[209][209];
int belong[209][209];
int b[209*209];
void Init()
{
for(int i=0;i<=N+1;i++)
for(int j=0;j<=M*4+1;j++)
A[i][j]=0;
for(int i=1;i<=N;i++)
for(int j=1,jj=1;j<=M;j++,jj+=4)
{
char r;
cin>>r;
int x;
if(r>='a'&&r<='z')
x=10+r-'a';
else
x=r-'0';
A[i][jj]=(x/8)%2;
A[i][jj+1]=(x/4)%2;
A[i][jj+2]=(x/2)%2;
A[i][jj+3]=(x/1)%2;
}
M*=4;
}
void Dfs0(int x,int y,int c)
{
if(x<0||y<0||x>N+1||y>M+1)
return ;
if(vis[x][y])
return ;
if(A[x][y]!=0)
return;
vis[x][y]=true;
belong[x][y]=c;
Dfs0(x+1,y,c);
Dfs0(x-1,y,c);
Dfs0(x,y+1,c);
Dfs0(x,y-1,c);
}
int Dfs1(int x,int y,int vt)
{
int ret=0;
if(x<0||y<0||x>N+1||y>M+1)
return 0;
if(vis[x][y])
return 0;
if(A[x][y]!=1)
{
if(b[belong[x][y]]!=vt)
ret++;
b[belong[x][y]]=vt;
return ret;
}
vis[x][y]=true;
ret+=Dfs1(x+1,y,vt);
ret+=Dfs1(x-1,y,vt);
ret+=Dfs1(x,y+1,vt);
ret+=Dfs1(x,y-1,vt);
return ret;
}
void Solve()
{
for(int i=0;i<=N+1;i++)
for(int j=0;j<=M+1;j++)
vis[i][j]=false;
int SpaceNum=0;
for(int i=0;i<=N+1;i++)
for(int j=0;j<=M+1;j++)
if(A[i][j]==0)
Dfs0(i,j,++SpaceNum);
for(int i=0;i<=N+1;i++)
for(int j=0;j<=M+1;j++)
vis[i][j]=false;
vector<char> Answer;
Answer.clear();
int vt=0;
for(int i=1;i<=SpaceNum;i++)
b[i]=-1;
for(int i=0;i<=N+1;i++)
for(int j=0;j<=M+1;j++)
if(A[i][j]==1)
{
int t=Dfs1(i,j,vt++);
if(t==1)
Answer.PB('W');
if(t==2)
Answer.PB('A');
if(t==3)
Answer.PB('K');
if(t==4)
Answer.PB('J');
if(t==5)
Answer.PB('S');
if(t==6)
Answer.PB('D');
}
sort(Answer.begin(),Answer.end());
for(int i=0;i<Answer.size();i++)
cout<<Answer[i];
cout<<"\n";
}
int main()
{
int Case=0;
while(cin>>N>>M&&(N||M))
{
Init();
cout<<"Case "<<++Case<<": ";
Solve();
}
return 0;
}