Description

Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. Nowadays, Aoshu is getting more and more difficult. Here is a classic Aoshu problem:

ABBDE __ ABCCC = BDBDE

In the equation above, a letter stands for a digit(0 � 9), and different letters stands for different digits. You can fill the blank with ‘+’, ‘-‘ , ‘×’ or ‘÷’.

How to make the equation right? Here is a solution:

12245 + 12000 = 24245

In that solution, A = 1, B = 2, C = 0, D = 4, E = 5, and ‘+’ is filled in the blank.

When I was a kid, finding a solution is OK. But now, my daughter’s teacher tells her to find all solutions. That’s terrible. I doubt whether her teacher really knows how many solutions are there. So please write a program for me to solve this kind of problems.

 

Input

The first line of the input is an integer T( T <= 20) indicating the number of test cases.

Each test case is a line which is in the format below:

s1 s2 s3

s1, s2 and s3 are all strings which are made up of capital letters. Those capital letters only include ‘A’,’B’,’C’,’D’ and ‘E’, so forget about ‘F’ to ‘Z’. The length of s1,s2 or s3 is no more than 8.

When you put a ‘=’ between s2 and s3, and put a operator( ‘+’,’-‘, ‘×’ or ‘÷’.) between s1 and s2, and replace every capital letter with a digit, you get a equation.

You should figure out the number of solutions making the equation right.

Please note that same letters must be replaced by same digits, and different letters must be replaced by different digits. If a number in the equation is more than one digit, it must not have leading zero.

 

Output

For each test case, print an integer in a line. It represents the number of solutions. 
 

Sample Input

2
A A A
BCD BCD B
 

Sample Output

5
72
 
暴力
卡题原因:全排列过后数字是完全打乱的,这时候直接选取新的组合是错误的,必须恢复原来的顺序
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int per[5],temper[5];
char left[10],right[10],equ[10];
int index[5];
int del(char ch){
return index[ch-'A'];
}
int getnum(char* str){
int ans=0;
for(int i=0;str[i];i++){
ans*=10;
ans+=per[del(str[i])];
}
return ans;
}
void cpy(int *a,int *b,int len){
for(int i=0;i<len;i++)a[i]=b[i];
}
void pr(int lnum,int rnum,int equ,int op){
/*printf("%d ",lnum);
if(op==0)putchar('+');
else if(op==1)putchar('-');
else if(op==2)putchar('*');
else if(op==3)putchar('/');
printf(" %d = %d\n",rnum,equ);*/
}
int check(){
int ans=0;
int lnum=getnum(left),rnum=getnum(right),eqnum=getnum(equ);
if(lnum+rnum==eqnum){pr(lnum,rnum,eqnum,0);ans++;}
if(lnum-rnum==eqnum){pr(lnum,rnum,eqnum,1);ans++;}
if(lnum*rnum==eqnum){pr(lnum,rnum,eqnum,2);ans++;}
if(rnum!=0&&lnum/rnum==eqnum&&lnum%rnum==0){pr(lnum,rnum,eqnum,3);ans++;}
return ans;
}
int dfs(int s,int ind,int rlen,int llen,int elen,int allen){
int ans=0;
per[s]=ind;
if(s==allen-1){
cpy(temper,per,allen);//注意记录原值,否则因为顺序是反的,就不能直接修改取的数字
if(!((per[del(left[0])]==0&&llen>1)||(per[del(right[0])]==0&&rlen>1)||(per[del(equ[0])]==0&&elen>1)))
ans+=check();
while(next_permutation(per,per+allen)){
// for(int i=0;i<allen;i++)printf("%d%c",per[i],i==allen-1?'\n':' ');
if((per[del(left[0])]==0&&llen>1)||(per[del(right[0])]==0&&rlen>1)||(per[del(equ[0])]==0&&elen>1))continue;
ans+=check();
}
cpy(per,temper,allen);
}
else {
for(int i=ind+1;i<10;i++)ans+=dfs(s+1,i,llen,rlen,elen,allen);
}
return ans;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s%s%s",left,right,equ);
int ans=0;
for(int i=0;i<5;i++)per[i]=i;
int llen=strlen(left),rlen=strlen(right),elen=strlen(equ);
memset(index,-1,sizeof(index));
int allen=0;
for(int i=0;left[i];i++)if(index[left[i]-'A']==-1)index[left[i]-'A']=allen++;
for(int i=0;right[i];i++)if(index[right[i]-'A']==-1)index[right[i]-'A']=allen++;
for(int i=0;equ[i];i++)if(index[equ[i]-'A']==-1)index[equ[i]-'A']=allen++;
for(int i=0;i<10;i++)ans+=dfs(0,i,llen,rlen,elen,allen);
printf("%d\n",ans);
}
return 0;
}

  

hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0的更多相关文章

  1. hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0

    H - National Day Parade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1

    F - Computer Virus on Planet Pandora Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format ...

  3. hdu 3697 10 福州 现场 H - Selecting courses 贪心 难度:0

    Description     A new Semester is coming and students are troubling for selecting courses. Students ...

  4. hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle 费马点 计算几何 难度:1

    In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the t ...

  5. hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0

    Description “Farm Game” is one of the most popular games in online community. In the community each ...

  6. hdu 3682 10 杭州 现场 C - To Be an Dream Architect 简单容斥 难度:1

    C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  7. hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0

    C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  8. hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1

    F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  9. hdu 3262 09 宁波 现场 C - Seat taking up is tough 暴力 难度:0

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

随机推荐

  1. CRM - 销售与客户

    一.销售与客户 - 表结构 ---公共客户(公共资源) 1.没有报名 2.3天没有跟进 3.15天没有成单 客户分布表 龙泰 男 yuan 2018-5-1 3天未跟进 龙泰 男 三江 2018-5- ...

  2. C# 反双曲余弦函数

    反双曲余弦函数的定义是: T1 = Math.Log(t + Math.Sqrt(t * t - 1)); 1. 叉乘(cross product),也叫向量的外积.向量积.顾名思义,求下来的结果是一 ...

  3. Spring boot 集成ckeditor

    1:下载ckeditor  4.4.2 full package ,官网没有显示, 需要在最新版本的ckeditor download右键,复制链接, 输入到导航栏,将版本号改为自己想要的版本号. h ...

  4. 统计编程的框架与R语言统计分析基础——摘(2)统计分析之线性回归

    一.线性回归 1.简单线性回归 a. > x = women > x height weight 1 58 115 2 59 117 3 60 120 4 61 123 5 62 126 ...

  5. sql2008 express 实现自动备份

    在一个项目中用到的数据库是sqlserver 2008 r2 express .可没想到express版本的功能有些限制,此前一直都不知道啊.百度百科可以看到它的限制: “1.数据库的大小限制:SQL ...

  6. 字节跳动冬令营网络赛 Solution

    A:Aloha Unsolved. B:Origami Unsolved. 题意: 初始的时候有一张纸,可以从左边往右边折叠,或者从右边往左边折叠 每次折叠的长度不能超过现有宽度,最后折叠到长度为1 ...

  7. echarts 不同区域背景色不同 废了我一天的时间

    var result = echarts.init(document.getElementById('result')); var option1 = { title: { text: '设备阶段故障 ...

  8. BabelMap 10.0.0.3 汉化版已经发布

    新的 BabelMap 在日前发布. 新版本增加了字符书签的管理功能,以及将窗口最小化到系统通知栏(时钟区域)的功能. 请点击主页左上角进入下载页面下载.

  9. Python: 矩阵与线性代数运算

    需要执行矩阵和线性代数运算,比如矩阵乘法.寻找行列式.求解线性方程组等等. 矩阵类似于3.9 小节中数组对象,但是遵循线性代数的计算规则.下面的一个例子展示了矩阵的一些基本特性: >>&g ...

  10. RedHot 不能联网 ifconfig命令只显示lo 不显示eth0的解决方法!

    1.修改eth0 具体步骤如下 vi /etc/sysconfig/network-scripts/ifcfg-eth0, i,进入insert编辑模式,具体参数设置如下: DEVICE=eth0 B ...