2010 Asia Fuzhou Regional Contest 之 A hard Aoshu Problem

A hard Aoshu Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 1289    Accepted Submission(s): 665


Problem 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<stdio.h>
#include<string.h>
typedef long long LL;
char s[30],s1[10],s2[10],s3[10];
int p[200],vis[200],n1,n2,n3,ans;
void dfs(int k)
{
    if(!s[k]){
        if(strlen(s1)>1 && p[s1[0]]==0)  return ;
        if(strlen(s2)>1 && p[s2[0]]==0)  return ;
        if(strlen(s3)>1 && p[s3[0]]==0)  return ;
        int len=0;
        n1=n2=n3=0;
        while(s1[len]){
            n1=n1*10+p[s1[len++]];
        }
        len=0;
        while(s2[len]){
            n2=n2*10+p[s2[len++]];
        }
        len=0;
        while(s3[len]){
            n3=n3*10+p[s3[len++]];
        }
        if(n1*n2==n3)   ans++;
        if(n1+n2==n3)   ans++;
        if(n1-n2==n3)   ans++;
        if(n2 && n1/n2==n3 && n1%n2==0)   ans++;
        return ;
    }
    if(p[s[k]]==-1){
        for(int i=0;i<=9;i++){
            if(!vis[i]){
                vis[i]=1;
                p[s[k]]=i;
                dfs(k+1);
                vis[i]=0;
                p[s[k]]=-1;
            }
        }
    }
    else    dfs(k+1);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        memset(p,-1,sizeof(p));
    memset(vis,0,sizeof(vis));
        scanf("%s%s%s",s1,s2,s3);
        strcpy(s,s1);
        strcpy(s+strlen(s),s2);
        strcpy(s+strlen(s),s3);
        ans=0;
        dfs(0);
        printf("%d\n",ans);
    }


    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎轩栀海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值