usaco 1.3.4 Prime Cryptarithm

本文探讨了一种解决使用特定数字集的加密乘法问题的方法,通过使用数组和逻辑判断,实现对所有可能解决方案的高效搜索。示例输入展示了如何利用此算法找到唯一解决方案,以及对代码优化和提交过程的反思。
Prime Cryptarithm

The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM.

      * * *
   x    * *
    -------
      * * *         <-- partial product 1
    * * *           <-- partial product 2
    -------
    * * * *
Digits can appear only in places marked by `*'. Of course, leading zeroes are not allowed.

Note that the 'partial products' are as taught in USA schools. The first partial product is the product of the final digit of the second number and the top number. The second partial product is the product of the first digit of the second number and the top number.

Write a program that will find all solutions to the cryptarithm above for any subset of digits from the set {1,2,3,4,5,6,7,8,9}.

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1:N, the number of digits that will be used
Line 2:N space separated digits with which to solve the cryptarithm

SAMPLE INPUT (file crypt1.in)

5
2 3 4 6 8

OUTPUT FORMAT

A single line with the total number of unique solutions. Here is the single solution for the sample input:

      2 2 2
    x   2 2
     ------
      4 4 4
    4 4 4
  ---------
    4 8 8 4

SAMPLE OUTPUT (file crypt1.out)

1


又开始无节操的看题解了。发现原来那个想法太2了。为什么自己就是想不到用数组设置10来判断是否是合法数这么简单的方法呢怒!还有枚举的时候不是枚举每个digit,而是枚举整个大数,这样乘法什么的也方便啊你个呆瓜!

/*
ID: wtff0411
PROG: crypt1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>

int n;
int c[10];

using namespace std;

bool isgood(int a,int m)
{
     while(m--)
     {
         if(!c[a%10]||a==0)
         return false;
         a/=10;
     }
     if(a!=0)return false;
     return true;
}

bool check(int i,int j)
{
     if(!isgood(i,3)||!isgood(j,2)||!isgood(i*j,4))
     return false;
     if(!isgood(i*(j%10),3)||!isgood(i*(j/10),3))
     return false;
     return true;
}
     


int main()
{
    freopen("crypt1.in","r",stdin);
    freopen("crypt1.out","w",stdout);
    
    cin>>n;
    int i;
    int j;
    int res=0;
    int fir[16];
    int input;
    for(i=0;i<10;i++)
    {
        c[i]=0;
    }
    for(i=0;i<n;i++)
    {
        cin>>input;
        c[input]=1;//why i can't think of that!
    }
    
    for(i=100;i<1000;i++)
        for(j=10;j<100;j++)
        {
            if(check(i,j))
            res++;
        }
    cout<<res<<endl;
    //system("pause");
    return 0;
}
        
    
    
        
    
    
    


今天翻墙成功,可以在寝室里上usaco了,也算一大收获吧。不过不知道是配置问题还是怎样,提交的时候总是说有问题,但是回主页刷新一下发现done了。唔,幸好是一遍过吧……其实这题想到方法真不难。。。想不到啊T^T……

。。法律好难……


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值