要点

  • 标签是dp但搜索一发就能过了。
  • 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5。
  • 显然要预处理有哪些素数。在这个过程中可以顺便再处理出一个\(vector:re[len][number]\),表示前面已经填了长度为len的数为number,那么最后会合法的填法应该在后面填什么数字。这么预处理之后会发现dfs时就很好枚举了。
  • 一个处理技巧是把数填在右下角而不是从坐标1开始填,这样我们的re数组就不用多加一维了:免去了目前矩阵的最大长度这一维。
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int maxn = 1e5 + 5;
int T, p, len, ans;
int vis[maxn], table[6][6];
vector<int> re[6][maxn]; void Fill(int st, int k) {
for (int i = 5; i >= st; i--, k /= 10) {
table[st][i] = table[i][st] = k % 10;
}
} void Pre() {
for (int i = 2; i < maxn - 5; i++) {
if (!vis[i]) {
for (int j = i * 2; j < maxn - 5; j += i)
vis[j] = 1;
for (int k = i, t = 1, j = 4; j; j--) {
t *= 10; k /= 10;
re[j][k].push_back(i % t);
}
}
}
} void dfs(int depth, int tmp = 0) {
if (depth > 5) {
ans++;
return;
} for (int i = 1; i < depth; i++)
tmp = tmp * 10 + table[depth][i];
for (int i : re[depth - 1][tmp]) {
Fill(depth, i);
dfs(depth + 1);
}
} int main() {
Pre();
for (scanf("%d", &T); T; T--) {
scanf("%d", &p);
memset(table, 0, sizeof table);
ans = len = 0;
for (int k = p; k; k /= 10, len++);
Fill(5 - len + 1, p);
dfs(5 - len + 2);
printf("%d\n", ans);
}
return 0;
}

Codeforces 161E(搜索)的更多相关文章

  1. Codeforces 754A(搜索)

    设s[i][j]为序列i到j的和,当s[i][j]≠0时,即可从i跳到j+1.目标为从1跳到n+1,所以按照题意暴力即可. #include <bits/stdc++.h> using n ...

  2. CodeForces 164A Variable, or There and Back Again 搜索

    Variable, or There and Back Again 题目连接: http://codeforces.com/problemset/problem/164/A Description L ...

  3. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  4. Codeforces Gym 100231G Voracious Steve 记忆化搜索

    Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...

  5. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  6. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

  7. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  8. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

  9. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

随机推荐

  1. JS遍历ChexkBoxList

    var cboxs = $("#cblAuth input[type=checkbox]"); ; ; i < cboxs.length; i++) { if (cboxs[ ...

  2. 「BZOJ2721」「LuoguP1445」 [Violet]樱花(数论

    题目背景 我很愤怒 题目描述 求方程 $\frac{1}{x}+\frac{1}{y}=\frac{1}{N!}$ 的正整数解的组数,其中$N≤10^6$. 解的组数,应模$1e9+7$. 输入输出格 ...

  3. 深入理解和探究Java类加载机制

    深入理解和探究Java类加载机制---- 1.java.lang.ClassLoader类介绍 java.lang.ClassLoader类的基本职责就是根据一个指定的类的名称,找到或者生成其对应的字 ...

  4. git repo

  5. Java虚拟机学习 - 体系结构 内存模型(转载)

    一:Java技术体系模块图 二:JVM内存区域模型 1.方法区 也称"永久代” .“非堆”,  它用于存储虚拟机加载的类信息.常量.静态变量.是各个线程共享的内存区域.默认最小值为16MB, ...

  6. C#Dos命令

    记下 打开IIs:按Win+R键,输入inetmgr ; 远程桌面:按Win+R键,输入mstsc; 启动服务:按Win+R键,输入inetmgr; 删除某服务:sc delete [ServerNa ...

  7. 将eclipse java程序打包成jar的总结(包括工程中没有引用外部jar包和有引用外部jar包两种情况)

    一.当eclispe java工程中没有引用外部jar包时: 选中工程---->右键,Export...--->Java--->JAR file--->next-->填写 ...

  8. 【244】◀▶IEW-Unit09

    Unit 9 Food 1)Model1题目及范文讲解 In the world today, there is a problem with food production. As a result ...

  9. Eclipse&nbsp;安装插件

    Eclipse 安装插件 本文介绍Eclipse插件的安装方法.Eclipse插件的安装方法大体有三种:直接复制.使用link文件,以及使用eclipse自带的图形界面的插件安装方法. AD: 做为当 ...

  10. eos表结构总结说明

    EOS6.0 WORKFLOW 表结构说明 流程定义表(WFProcessDefine) 名称 代码 描述 流程定义ID processDefID 主键 流程定义名称 processDefName 业 ...