题意:给你一串数字,最少一个,最多七个,问用这里面的数字能组成多少素数,不重复。

思路:之前还遍历10000000的每一个素数,结果超时,后来发现直接dfs就可以了,只是标记一下做过的数。

 #pragma comment(linker, "/STACK:1000000000")
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt", "w", stdout)
#define MAXN 10000005
using namespace std;
bool vis[MAXN], done[MAXN], has[];
int p[];
char s[];
int cnt[], a[];
int n, ans;
void dfs(int m, int res){
if(done[res]) return;
if(!vis[res]){
ans++;
}
done[res] = true;
if(m > n) return;
for(int i = ; i <= n; i++){
if(has[i]) continue;
has[i] = true;
dfs(m + , res * + (s[i] - ''));
dfs(m + , res);
has[i] = false;
}
}
int main()
{
//IN;
//OUT;
int T;
memset(vis, , sizeof(vis));
int o = ;
vis[] = vis[] = true;
for(int i = ; i <= ; i++){
if(!vis[i]){
p[o++] = i;
}
for(int j = * i; j <= ; j += i){
vis[j] = true;
}
}
scanf("%d", &T);
while(T--){
//memset(cnt, 0, sizeof(cnt));
ans = ;
memset(has, , sizeof(has));
memset(done, , sizeof(done));
scanf("%s", s + );
n = strlen(s + );
dfs(, );
printf("%d\n", ans);
}
return ;
}

POJ - 3842 An Industrial Spy dfs(水)的更多相关文章

  1. POJ -- 3842

    An Industrial Spy   Description Industrial spying is very common for modern research labs. I am such ...

  2. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  3. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  4. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  5. POJ 3256 DFS水题

    枚举点 每次都搜一遍 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ...

  6. poj 1321 棋盘问题 简单DFS

    题目链接:http://poj.org/problem?id=1321 很久没有敲搜索了啊,今天敲了个水题练练手,哈哈.... 题目大意: 就是求在n*n的方格上放置k个棋子的方案数 代码: #inc ...

  7. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

  8. 【wikioi】1229 数字游戏(dfs+水题)

    http://wikioi.com/problem/1229/ 赤裸裸的水题啊. 一开始我认为不用用完全部的牌,以为爆搜会tle.. 可是我想多了. 将所有状态全部求出,排序后暴力判断即可. (水题有 ...

  9. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

随机推荐

  1. 关于Subversion主从备份方式的调整(全量、增量脚本)更新

    本文引用于http://blog.chinaunix.net/uid-25266990-id-3369172.html 之前对Subversion服务器作了迁移,关于SVN的架构也走了调整,有单一的服 ...

  2. 紫书 习题 11-8 UVa 1663 (最大流求二分图最大基数匹配)

    很奇怪, 看到网上用的都是匈牙利算法求最大基数匹配 紫书上压根没讲这个算法, 而是用最大流求的. 难道是因为第一个人用匈牙利算法然后其他所有的博客都是看这个博客的吗? 很有可能-- 回归正题. 题目中 ...

  3. 紫书 习题8-8 UVa 1612 (贪心+精度)

    这道题我很快就写出来了, 但是一直WA, 然后发现是精度, 这坑了我一个小时-- (1)贪心.每次就尽量分数高, 可以保证最后分数最高 (2)神tm精度问题.记住判断大于小于和等于的时候要用EPS(1 ...

  4. PID的原理

    来源:https://www.cnblogs.com/foxclever/p/8902029.html 在自动控制中,PID及其衍生出来的算法是应用最广的算法之一.各个做自动控制的厂家基本都有会实现这 ...

  5. Android基础笔记(十三)- 内容提供者原理和简单使用

    为什么要有内容提供者 内容提供者的工作原理 使用内容解析者对内容提供者进行增删改查操作 利用内容提供者和内容解析者备份手机短信 利用内容提供者插入短信 为什么要有内容提供者 内容提供者技术的目的是: ...

  6. angularjs 自定义服务

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  7. vs输出窗口,显示build的时间

    https://stackoverflow.com/questions/82128/displaying-build-times-in-visual-studio Tools... Options.. ...

  8. 监控rman备份

    1.服务会话关联通道设置 set COMMAND ID 命令 2.查询V$PROCESS和V$SESSION 决定会话对应的RMAN的通道 3.查询V$session_LONGGOPS监控备份集和复制 ...

  9. 51Nod 最长的循环节(打表预处理)

    正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数,假如存在多个最优的答案,输出所有答案中最大的那个数.     1/6= ...

  10. 并查集 (Union Find ) P - The Suspects

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...