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 ...
随机推荐
- FUSE
FUSE is particularly useful for writing [ vritual ] file system. Unlike traditional filesystem that ...
- JSP基本语法--包含指令<%@include file="路径"%> <jsp:include page>
包含指令,真正改变的地方只有具体内容处: 方法1: 在每个jsp页面(HTML)都包含工具栏,头部信息,尾部信息,具体内容 方法2: 将工具栏,头部信息,尾部信息都分成各个独立的文件,使用的时候直接导 ...
- 使用composer命令创建laravel项目命令详解
composer命令创建laravel项目的命令是: composer create-project --prefer-dist laravel/laravel blog "5.2.*&qu ...
- postfix队列管理
队列管理单元的服务器程序--qmgr,是整个postfix系统的中心枢纽.所有邮件,包括等待送出与从外界收进来的,都必须通过队列.了解队列的运行原理以及postfix如何处理队列,有助于你解决问题. ...
- Java-Spring MVC如何返回一个非JSP文件名字的地址
return new ModelAndView("redirect:/bizitem/goEditItem.do?item_id="+item_id+"&msg= ...
- cocos2d-x3.x Vector
auto sp0 = Sprite::create(); sp0->setTag(); auto sp1 = Sprite::create(); sp1->setTag(); //这里使用 ...
- Git学习 -- 新建版本库
创建目录 mkdir git cd git 2 初始化git init #会在目录下自动创建一个.git目录,用于跟踪管理版本库,不要修改 3 向版本库中添加文件git add readme.txt ...
- vbs 截图
'VBS截屏.vbs ' Win7x64 测试通过(已安装Word2007): '参考: ' http://qtp.blogspot.com/2010/02/screenshot-vbscript ...
- CentOS服务端口开放
#例如开放81端口iptables -I INPUT -i eth0 -p tcp --dport 81 -j ACCEPTiptables -I OUTPUT -o eth0 -p tcp --sp ...
- hdu_1007_Quoit Design(最近点对)
题目连接:hdu_1007_Quoit Design 题意: 给你平面上的一些点,让你找出这些点的最近点对的距离 题解: 采用分治,达到O(nlognlogn)的时间复杂度就能艹过去了 #includ ...