HDU3699 A hard Aoshu Problem

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


2010fuzhou

模拟。注意不要让没出线的字符也被枚举了,以及整数相除相等的比较需要注意。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
//const double eps=1e-7;
//const double INF=1e50;
//const double pi=acos(-1);

#define N 1005
#define M 20005

char s1[10],s2[10],s3[10];
int a[6],sum;
bool b[10],ss[6];

long long value(char f[])
{
    int l=strlen(f);

    long long ans=0;
    int i,w=1;
    for (i=l-1;i>=0;i--)
    {
        ans+=a[(int)(f[i]-'A')]*w;
        w*=10;
    }

    return ans;
}

int check()
{
    int l1,l2,l3;
    l1=strlen(s1);
    l2=strlen(s2);
    l3=strlen(s3);

    if (l1>1 && a[(int)(s1[0]-'A')]==0) return 0;
    if (l2>1 && a[(int)(s2[0]-'A')]==0) return 0;
    if (l3>1 && a[(int)(s3[0]-'A')]==0) return 0;

    long long a1,a2,a3;
    int ans=0;
    a1=value(s1);
    a2=value(s2);
    a3=value(s3);

    if (a1+a2==a3) ans++;
    if (a1-a2==a3) ans++;
    if (a1*a2==a3) ans++;
    if (a2!=0 && ( fabs((double)a1/(double)a2-(double)a3)<0.00001)) ans++;//not a1/a2==a3

    //if (ans>0) cout<<a1<<' '<<a2<<' '<<a3<<endl;
    return ans;
}

void init()
{
    int i,l;
    for (i=0;i<5;i++) ss[i]=false;
    for (i=0;i<=9;i++) b[i]=true;

    l=strlen(s1);
    for (i=0;i<l;i++) ss[(int)(s1[i]-'A')]=true;
    l=strlen(s2);
    for (i=0;i<l;i++) ss[(int)(s2[i]-'A')]=true;
    l=strlen(s3);
    for (i=0;i<l;i++) ss[(int)(s3[i]-'A')]=true;

    sum=0;
}

void ser(int m)
{
    if (m==5)
    {
        sum+=check();
        return ;
    }

    if (ss[m]==false) ser(m+1);
    else
    {
        for (a[m]=0;a[m]<=9;a[m]++)
        if (b[a[m]]==true)
        {
            b[a[m]]=false;
            ser(m+1);
            b[a[m]]=true;
        }
    }
}

int main()
{
   // freopen("a","r",stdin);

    int T,kk;
    scanf("%d\n",&T);

    for (kk=1;kk<=T;kk++)
    {
        scanf("%s %s %s\n",s1,s2,s3);
        init();

        ser(0);
        printf("%d\n",sum);
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值