Home > Old Blog Posts > USACO 1.3.4 Prime Cryptarithm

USACO 1.3.4 Prime Cryptarithm


暴力吧……反正只用枚举5个数

这道题能超时的都是帝

注意的是,中间的计算结果也要都由这5个数组成

代码:

/*
ID: dementr1
PROG: crypt1
LANG: C++
*/
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin(“crypt1.in”);
ofstream fout(“crypt1.out”);
int n,data[10];
void init()
{
int i;
fin>>n;
for(i=0;i<n;i++) fin>>data[i];
}
bool search(int a)
{
int i;
for(i=0;i<n;i++) if(data[i]==a) return true;
return false;
}
void change(int a, int b[])
{
int num=0,t=a;
while(t>0)
{
b[num++]=t%10;
t/=10;
}
}
bool check(int a, int b, int c, int d, int e)
{
int up=(100*a+10*b+c)*e,
down=(100*a+10*b+c)*d,i;
if(100<=up&&up<=999&&100<=down&&down<=999&&1000<=up+down*10)
{
int a[3],b[3];
change(up,a);
change(down,b);
for(i=0;i<3;i++)
{
if(!search(a[i])||!search(b[i])) return false;
}
int c[4];
change(up+down*10,c);
for(i=0;i<4;i++) if(!search(c[i])) return false;
}
else return false;
return true;
}
void work()
{
int i,j,k,l,m,ans=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
for(l=0;l<n;l++)
for(m=0;m<n;m++)
if(check(data[i],data[j],data[k],data[l],data[m])) ans++;
fout<<ans<<endl;
}
int main()
{
init();
work();
return 0;
}

Categories: Old Blog Posts
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: