要点

  • 标签是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. hadoop源码剖析--hdfs安全模式

    一.什么是安全模式 hadoop安全模式是name node的一种状态,处于该状态时有种量特性: 1.namenode不接受任何对hfds文件系统的改变操作(即此时整个文件系统处于只读状态): 2.不 ...

  2. C语言的内存四区模型和函数调用模型

    首先是操作系统将代码程序加载到内存中 然后将内存分为4个区 栈区,程序的局部变量区,函数传递的参数,由编译器自动进行内存资源的释放. 堆区,动态内存申请,如果不手动释放内存,则这块内存不会进行析构. ...

  3. CodeForces - 311B:Cats Transport (DP+斜率优化)

    Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight r ...

  4. HihoCoder1656 : 前缀后缀查询([Offer收割]编程练习赛39)(字典树+小技巧)

    描述 给定一个包含N个单词的字典:{W1, W2, W3, ... WN},其中第i个单词Wi有具有一个权值Vi. 现在小Hi要进行M次查询,每次查询包含一个前缀字符串Pi和一个后缀字符串Si.他希望 ...

  5. 洛谷 U6931 灯光

    题目背景 明天就是校园活动了,小明作为场地的负责人,将一切都布置好了.但是在活动的前几天,校园里的灯却都坏掉了,无奈之下,只好再去买一批灯.但是很遗憾的是,厂家看马上要过年了,就没有在进货了,现在只剩 ...

  6. Html 5 简介

    html5 是下一代的html 什么是 html5? html5 将成为 html.xhtml 以及 html dom 的新标准. html 的上一个版本诞生于 1999 年.自从那以后,web 世界 ...

  7. node.js setup wizard ended prematurely Win7安装nodejs失败解决方法

    笔记本win7在nodejs官方网站下载.msi文件安装,安装到一半的时候,进度条提示:roll back,because of a error.node.JS setup wizard ended ...

  8. Java的Socket通信(多线程)(1)

    如图: 思路: ①首先创建服务器端Socket,指定并侦听某一个端口,然后循环监听开始等待客户端的连接…. ②创建客户端socket,指定服务器地址和端口,然后获取输出流,向服务器端发送请求,并关闭s ...

  9. Word直接发表博客测试

    这是我现在使用的VSCode! 这是另一段测试内容!

  10. codec can't decode byte 0xe6 in position 0: ordinal not in range

    ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)] 错误: 实例 "linux-cor ...