hdu3699 A hard Aoshu Problem 暴搜

http://acm.hdu.edu.cn/showproblem.php?pid=3699

A hard Aoshu Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others)

Total Submission(s): 968    Accepted Submission(s): 490

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

Source

2010 Asia Fuzhou Regional Contest

题意很简单,都不知道咋描述。。

就是爆搜下,注意下前导0以及除法别直接除。。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-8;
const double pi=acos(-1.0);
const int INF=0x7fffffff;
const LL inf=(((LL)1)<<61)+5;
map<char,int>mp;
char s[3][10];
int val[3][10];
int a[10];
bool vis[10];
int ans;
int gao(int x)
{
    int ans=0,len=strlen(s[x]);
    for(int i=0;i<len;i++)
        ans=ans*10+a[val[x][i]];
    return ans;
}
void dfs(int step)
{
    if(step==0)
    {
        if(a[val[0][0]]==0&&strlen(s[0])>1)
            return;
        if(a[val[1][0]]==0&&strlen(s[1])>1)
            return;
        if(a[val[2][0]]==0&&strlen(s[2])>1)
            return;
        int x=gao(0),y=gao(1),z=gao(2);
        if(x+y==z) ans++;
        if(x-y==z) ans++;
        if((LL)x*(LL)y==z) ans++;
        if(y!=0&&(LL)z*(LL)y==x) ans++;
        return;
    }
    for(int i=0;i<10;i++)
    {
        if(!vis[i])
        {
            vis[i]=true;
            a[step]=i;
            dfs(step-1);
            vis[i]=false;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        clr(vis);
        mp.clear();
        clr(val);
        int num=1;
        ans=0;
        for(int i=0;i<3;i++)
        {
            scanf("%s",s[i]);
            int len=strlen(s[i]);
            for(int j=0;j<len;j++)
            {
                if(mp[s[i][j]]==0)
                {
                    mp[s[i][j]]=num++;
                    val[i][j]=mp[s[i][j]];
                }
                else val[i][j]=mp[s[i][j]];
            }
        }
        dfs(num-1);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2025-01-01 10:42:04

hdu3699 A hard Aoshu Problem 暴搜的相关文章

hdu5323Solve this interesting problem 暴搜

//给一对数[l,r] //问找出最小的n使得线段树的根节点的左右范围是[0,n],且 //该线段树中有左右范围为[l,r]的节点 //由于l/(r-l+1)≤2015 //可以直接暴力搜索以[l,r]为节点的其父亲节点的情况 //然后比较其最小值 #include<iostream> #include<cstring> #include<cstdio> using namespace std ; typedef long long ll ; const __int64

[ACM] hdu 4403 A very hard Aoshu problem (DFS暴搜数字)

A very hard Aoshu problem Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a pro

HDU4403(暴搜)

A very hard Aoshu problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4403 Description Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary m

hdu 4403 A very hard Aoshu problem

hdu 4403 A very hard Aoshu problem 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 DFS 这几天集训,一天也就写个4题,被虐哭QAQ.回寝室后游少说解搜索就大胆搜,最后剪个枝就好了Orz,然后我就尝试解这题(剪枝要风骚).我先枚举等号的位置equ,然后搜索加号add的位置,然后主要的剪枝:如果等号左边运算后小于等号右边各个位上的数的和,那么后面的肯定不满足条件,右边同理.最后要小心爆int,这里吃了很多W

HDU 5012 bfs暴搜

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 243    Accepted Submission(s): 135 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number wa

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo

hdu 4848 Wow! Such Conquering! (暴搜+强剪枝)

Wow! Such Conquering! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space

hdu 5348 MZL&#39;s endless loop 暴搜

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1426    Accepted Submission(s): 319 Special Judge Problem Description As w

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s