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 ...
随机推荐
- R语言笔记4--可视化
接R语言笔记3--实例1 R语言中的可视化函数分为两大类,探索性可视化(陌生数据集,不了解,需要探索里面的信息:偏重于快速,方便的工具)和解释性可视化(完全了解数据集,里面的故事需要讲解别人:偏重全面 ...
- listview前几个item的图片怎么是空白的、listview更新了ui不起作用、在handler里更新了UI不起作用
不是不起作用,不信你可以在更新ui代码附近加输出的log,说明程序是跑到那里了.但是未达到我们的想要的效果. 我们知道在listview里更新UI,listview的适配器Adapter里有个getV ...
- MySQL 对于千万级的大表要怎么优化
转自知乎 作者:哈哈链接:http://www.zhihu.com/question/19719997/answer/81930332来源:知乎著作权归作者所有,转载请联系作者获得授权. 很多人第一反 ...
- Eclipse的Console乱码
1.找到服务器bin目录:例:D:\WebLogic_11g\Middleware\user_projects\domains\dsrhd_domain\bin, 在该目录下找到setDomainEn ...
- queue(),dequeue()
这两个方法,一个是往里面添加队列,一个是执行队列 也是分静态方法和实例方法, 同样,实例方法最后调用静态方法 源码主要分析一下延迟delay方法,如何起作用的,写的有点仓促,先记录一下 在这里参照了网 ...
- 首次编译TI Android JB-4.2.2-DevKit-4.1.1的时候提示jdk版本不对
http://processors.wiki.ti.com/index.php/TI-Android-JB-4.2.2-DevKit-4.1.1_DeveloperGuide#Configure_An ...
- 转载,find.sh
#!/bin/bash #find files contains a keyword #write by xiaojing.zhao #2012.12.14 echo -e "\nThis ...
- Echarts自适应浏览器大小
var myChart = echarts.init(document.getElementById('sitesChar')); var option = { title : { text: 'No ...
- HDU 3732 Ahui Writes Word 多重背包优化01背包
题目大意:有n个单词,m的耐心,每个单词有一定的价值,以及学习这个单词所消耗的耐心,耐心消耗完则,无法学习.问能学到的单词的最大价值为多少. 题目思路:很明显的01背包,但如果按常规的方法解决时间复杂 ...
- 50条规则提高PHP开发提高效率技巧
0.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作 参数的“函数”(译注:PHP手 ...