USACO 1.3.3 Prime Cryptarithm
我用的枚举法,即每产生一组数据就判断是否是所给数字里的.
AC还沾沾自喜,但一看题解,发现自己的代码真low...
在平时练习时,应该追求高效,精炼的代码,这样比赛时才能省出大量时间去做其他题!
/*
ID:wang9621
PROG:crypt1
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int cnt[];
int main()
{
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
int n;
scanf("%d",&n);
for(int i = ;i<=n; i++) scanf("%d",&cnt[i]);
int count = ;
int flag = ;
int flag1 = ;
int flag2 = ;
int cnt1,cnt2;
for(int i = ; i<=n; i++)
{
if(cnt[i]==&&i==) continue;
for(int j = ; j<=n; j++)
{
for(int k = ; k<=n; k++)
{
flag = cnt[i]*+cnt[j]*+cnt[k];
for(int p = ; p<=n; p++)
{
if(cnt[p]==&&p==) continue;
for(int q = ; q<=n; q++)
{
cnt1 = flag1 = flag*cnt[p];
cnt2 = flag2 = flag*cnt[q];
int flag11 = ;
int wei1 = ;
int flag22 = ;
int wei2 = ;
while(flag1)
{
int sou = flag1%;
for(int s = ; s<=n; s++)
{
if(cnt[s] == sou)
{
flag11++;
break;
}
}
wei1++;
flag1 /= ;
}
while(flag2)
{
int sou = flag2%;
for(int s = ; s<=n; s++)
{
if(cnt[s] == sou)
{
flag22++;
break;
}
}
wei2++;
flag2 /= ;
}
if(flag11==&&flag22==&&wei1==&&wei2==)
{
int sum = cnt1+cnt2*;
int cntt = ;
int cnttt = ;
while(sum)
{
int sou = sum%;
for(int s = ; s<=n; s++)
{
if(cnt[s] == sou)
{
cntt++;
break;
}
}
cnttt++;
sum /= ;
}
if(cntt==&&cnttt==)
{
count++;
}
}
}
}
}
}
}
printf("%d\n",count);
return ;
}
我的代码
测试结果
Executing...
Test 1: TEST OK [0.000 secs, 4184 KB]
Test 2: TEST OK [0.000 secs, 4184 KB]
Test 3: TEST OK [0.000 secs, 4184 KB]
Test 4: TEST OK [0.000 secs, 4184 KB]
Test 5: TEST OK [0.011 secs, 4184 KB]
Test 6: TEST OK [0.000 secs, 4184 KB]
Test 7: TEST OK [0.011 secs, 4184 KB] All tests OK.
/*
ID:wang9621
PROG:crypt1
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int cnt[];
bool hash1(int x)
{
while(x)
{
if(!cnt[x%]) return false;
x /= ;
}
return true;
}
int main()
{
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
int n,x;
int count = ;
scanf("%d",&n);
for(int i = ;i<=n; i++)
{
scanf("%d",&x);
cnt[x] = ;
}
for(int i = ; i<; i++)
{
if(hash1(i))
{
for(int j = ; j<; j++)
{
if(hash1(j))
{
if(i*j<&&i*(j/)<&&i*(j%)<&&hash1(i*(j%))&&hash1(i*(j/))&&hash1(i*j))
{
count++;
}
}
}
}
}
printf("%d\n",count);
return ;
}
高效代码
测试结果 Executing...
Test 1: TEST OK [0.000 secs, 4180 KB]
Test 2: TEST OK [0.000 secs, 4180 KB]
Test 3: TEST OK [0.000 secs, 4180 KB]
Test 4: TEST OK [0.000 secs, 4180 KB]
Test 5: TEST OK [0.000 secs, 4180 KB]
Test 6: TEST OK [0.000 secs, 4180 KB]
Test 7: TEST OK [0.000 secs, 4180 KB]
All tests OK.
USACO 1.3.3 Prime Cryptarithm的更多相关文章
- USACO Section 1.3 Prime Cryptarithm 解题报告
题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...
- USACO 1.3.4 Prime Cryptarithm 牛式(模拟枚举)
Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...
- 洛谷P1211 [USACO1.3]牛式 Prime Cryptarithm
P1211 [USACO1.3]牛式 Prime Cryptarithm 187通过 234提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 题面错误 题目描述 ...
- l洛谷——P1211 [USACO1.3]牛式 Prime Cryptarithm
P1211 [USACO1.3]牛式 Prime Cryptarithm 题目描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x ** ...
- 洛谷 P1211 [USACO1.3]牛式 Prime Cryptarithm
P1211 [USACO1.3]牛式 Prime Cryptarithm 题目描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x ** ...
- USACO Section1.3 Prime Cryptarithm 解题报告
crypt1解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- 【USACO题库】1.3.4 Prime Cryptarithm牛式
好久没有发题解了,今天发一个很久很久之前写过得题吧 题目其实莫名的难 但是理解后,原来就是一只纸老虎 题目加工中~~~~(缩短题目) 加工完成:已知数字1-9组成集合的一个子集,求满足题意乘法步骤的情 ...
- 【USACO 1.5】Prime Palindromes
/* TASK: pprime LANG: C++ SOLVE: 枚举数的长度,dfs出对称的数,判断是否在范围内,是否是素数 原来想着枚举每个范围里的数,但是显然超时,范围最大是10^9. 对称的数 ...
- p1211 Prime Cryptarithm
直接深搜+检验. #include <iostream> #include <cstdio> #include <cmath> #include <algor ...
随机推荐
- IT人为什么难以拿到高薪?【转帖】
最近在论坛里看到很多人发牢骚,说薪水少,可在我看来,你们这样的人拿得到高薪才怪! 我先问一句:这里有多少人是本科的?有多少人是正规本科的(不算自考,成考和专升本)?有多少人是有学位的?有多少有学位的是 ...
- 安装apache服务出错,无法启动此程序,因为计算机中丢失VCRUNTIME140.dll 尝试重新安装此程序以解决此问题
错误信息:无法启动此程序,因为计算机中丢失VCRUNTIME140.dll 尝试重新安装此程序以解决此问题 错误场景:在使用["D:\Program Files\httpd-2.4.20-x ...
- Objective-C对象模型及应用
引言 简介 与Runtime交互 Runtime术语 消息 动态方法解析 消息转发 健壮的实例变量(Non Fragile ivars) Objective-C Associated Objects ...
- MyBatis-执行插入语句的时候返回主键ID到传入的参数对象中
<!-- 保存项目信息 --> <insert id="saveItem" parameterType="pd" useGeneratedKe ...
- table详解
1.tr 元素定义表格行,th 元素定义表头,td 元素定义表格单元. tr内是th还是td可由自己定义,th,td可存在于任一行,th与td的区别在与th字体更粗 2.定义一个table默认有bor ...
- opatch auto in windows db in 11.2.0.4
--prapare:copy 192.168.63.83 D:\oracle_patch\1612 to 192.168.2.169 D:\oracle_patch\1612cd D:\oracle_ ...
- CSS3的一些前缀
为了兼容多个浏览器,css3通常前面加一大堆前缀 -webkit /*为Chrome/Safari*/-moz /*为Firefox*/-ms /*为IE*/-o /*为Opera*/ -w ...
- 【单源最短路】dijstra poj 1502
#include <cstdio> #include <iostream> #include <stdlib.h> #include <memory.h> ...
- dos命令(Cacls和Icacls) -- 显示或者修改文件的访问控制表
1. dos帮助说明 cacls /? 注意: 不推荐使用 Cacls,请使用 Icacls. 显示或者修改文件的访问控制列表(ACL) CACLS filename [/T] [/M] [/L] [ ...
- opencv---cvor
void cvXor计算两个数组中的每个元素的按位异或. void cvXor (const CvArr* src1, const CvArr* src2, CvArr* dst, const CvA ...