要点

  • 标签是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. python的join()函数

    def join(self, iterable): # real signature unknown; restored from __doc__ """ S.join( ...

  2. C#高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)

    网址:http://blog.csdn.net/zhujunxxxxx/article/details/43573879 引言 我一直在探寻一个高性能的Socket客户端代码.以前,我使用Socket ...

  3. redis数据

    毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...

  4. swoole+nginx反向代理

    nginx配置: server { listen 80; server_name www.swoole.com; root /data/wwwroot/www.swoole.com; location ...

  5. java数据的5种存储位置(转)

    任何语言所编写的程序,其中的各类型的数据都需要一个存储位置,java中书的存储位置分为以下5种: 1.寄存器 最快的存储区,位于处理器内部,但是数量及其有限.所以寄存器根据需求自动分配,无序人为控制. ...

  6. Go丨语言package github.com/Go-SQL-Driver/MySQL: exec: "git": executable file not found in %PATH%解决方法

    Go语言在添加第三方MySQL驱动的时候报错: go: missing Git command. See https://golang.org/s/gogetcmd package github.co ...

  7. 1>/dev/null 2>&1 & 意思解析

    原文:https://jingyan.baidu.com/article/6dad5075334e26a123e36e31.html 用 /dev/null 2>&1 这样的写法.这条命 ...

  8. linux 进程学习笔记-信号semaphore

    信号灯(信号量)不是进程通信手段,其是用于控制和协调在进程间通信过程中的共享资源访问,就如同互斥锁(两者的区别可以参考这里) 可以将简单地将信号灯想象成一个计数器,初始时计数器值为n(有n个资源可供使 ...

  9. nginx 轮询模式 nginx_upstream_jvm_route 插件安装

    使用nginx_upstream_jvm_route插件的目的是为了保证在轮询机制下的session的共享 前提:源码方式安装nginx.patch命令 1.下载nginx_upstream_jvm_ ...

  10. Set connectionId threw an exception.

    今天调试一个WPF程序时,出现一个问题. 程序运行后抛出异常, "Set connectionId threw an exception. XXXXXXXXXX",原因是依赖的一个 ...