zoukankan      html  css  js  c++  java
  • HDOJ3699 A hard Aoshu Problem[暴力]

    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

    题意:给三个串s1,s2,s3,长度最多是8,由ABCDE五个字母组成,每个字母代表不同的数字,问能使s1+s2=s3或s1-s2=s3或s1*s2=s3或s1/s2=s3成立的方案有多少种。注意不能有前导零。

    只需枚举每个字母代表什么数字即可,某个字母不存在就把它跳过。


    #include<iostream>
    #include<cassert>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<iterator>
    #include<cstdlib>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    #define debug(x) cout<<"debug "<<x<<endl;
    #define rep(i,f,t) for(int i = (f),_end_=(t); i <= _end_; ++i)
    #define rep2(i,f,t) for(int i = (f),_end_=(t); i < _end_; ++i)
    #define dep(i,f,t) for(int i = (f),_end_=(t); i >= _end_; --i)
    #define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i)
    #define clr(c, x) memset(c, x, sizeof(c) )
    typedef long long int64;
    const int INF = 0x5f5f5f5f;
    const double eps = 1e-8;
    
    
    //*****************************************************
    
    int num[128];  //某个字母代表的数字,-1表示该字母不存在.
    char a[6],b[6],c[6];
    string s1,s2,s3;
    int n1,n2,n3;
    bool ok(int i){
        dep(j,i-1,'A')if(num[j] == num[i])return false;
        return true;
    }
    bool zerook(int i){
        if(s1[0]==i&&s1.size()!=1 || s2[0]==i&&s2.size()!=1 || s3[0]==i&&s3.size()!=1)return false;
        return true;
    }
    int dfs(int i)
    {
        if(i == 'F'){
            n1 = n2 = n3 = 0;
            rep(j,0,s1.size()-1){
                n1 *= 10;
                n1 += num[s1[j]];
            }
            rep(j,0,s2.size()-1){
                n2 *= 10;
                n2 += num[s2[j]];
            }
            rep(j,0,s3.size()-1){
                n3 *= 10;
                n3 += num[s3[j]];
            }
            int ans = 0;
            if(n1+n2 == n3)++ans;
            if(n1-n2 == n3)++ans;
            if(n1*n2 == n3)++ans;
            if(n2 && n1%n2==0 && n1/n2 == n3)++ans;   //注意要整除
            return ans;
        }
        if(num[i] == -1){
            return dfs(i+1);
        }else{
            int ans = 0;
            num[i] = 0;
            if(!zerook(i))++num[i];
            for(; num[i] < 10; ++num[i]){
                if(ok(i)){
                    ans += dfs(i+1);
                }
            }
            return ans;
        }
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            clr(num,-1);
            scanf("%s%s%s",a,b,c);
            s1 = a;s2 = b;s3 = c;
            rep(i,0,s1.size()-1)num[s1[i]] = 0;
            rep(i,0,s2.size()-1)num[s2[i]] = 0;
            rep(i,0,s3.size()-1)num[s3[i]] = 0;
    
            int ans = dfs('A');
            printf("%d
    ",ans);
        }
        return 0;
    }
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    TextSharp详情
    Java使用Itext5.5.10进行pdf签章
    java生成PDF,各种格式、样式、水印都有
    使用itext直接替换PDF中的文本
    在Asp.Net中操作PDF – iTextSharp
    使用iTextSharp修改PDF文件(一)
    微信公众平台HTTPS方式调用配置免费https服务器
    ef SQL Server 版本不支持数据类型“datetime2”
    开源框架.netCore DncZeus学习(三)增加一个菜单
    开源框架.netCore DncZeus学习(二)配置连接
  • 原文地址:https://www.cnblogs.com/DSChan/p/4861998.html
Copyright © 2011-2022 走看看