PRIMPERM - Prime Permutations
将题目分解成两个部分:
判断素数
如果用暴力筛因子的方法,在 $t \le 10^4,n \le 10^7$ 下肯定是要超时的,所以用了时间和空间都比较廉价的埃氏筛法。
代码:
bool f[10000010];//值为1不是质数。
void init(){
f[0]=f[1]=1;//0,1不是质数。
for(int i=2;i<=10000000;i++){
if(!f[i]){
for(int j=2*i;j<=10000000;j+=i){
f[j]=1;//质数的倍数不是质数。
}
}
}
}
全排列
手写全排列太麻烦了,所以这里用函数 next_permutation,用法:next_permutaion(数组名+起始地址,数组名+末尾地址+1),注意使用之前要对数组进行升序排序。
代码(前面都讲清楚了,不放注释):
#include<bits/stdc++.h>
using namespace std;
int w[11],prime[10000010];
bool f[10000010];
void init(){
f[0]=f[1]=1;
for(int i=2;i<=10000000;i++){
if(!f[i]){
for(int j=2*i;j<=10000000;j+=i){
f[j]=1;
}
}
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
init();
int T;
cin>>T;
while(T--){
int n,m=0,ans=0;
cin>>n;
while(n!=0){
w[++m]=n%10;
n/=10;
}
sort(w+1,w+1+m);
do{
if(!w[1]) continue;
n=0;
for(int i=1;i<=m;i++){
n=n*10+w[i];
}
ans+=!f[n];
}while(next_permutation(w+1,w+m+1));
cout<<ans<<endl;
}
return 0;
}
PRIMPERM - Prime Permutations的更多相关文章
- (Problem 49)Prime permutations
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...
- one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏
one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...
- 【leetcode】1175. Prime Arrangements
题目如下: Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-inde ...
- D. Number Of Permutations 符合条件的排列种类
D. Number Of Permutations time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Prime Generator
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
随机推荐
- css设置backgroud:url(),无效
react项目中,使用styled-components编写样式,给元素添加背景图不生效. background直接设置十六进制颜色或者颜色的英文名称都是可行的,但是使用url无作用,那就是url问题 ...
- vuepress-reco搭建与部署指南
个人博客:槿苏的知识铺 一.前言 在技术飞速发展的今天,高效地编写.维护和呈现文档已成为开发者不可或缺的能力.无论是开源项目.团队协作还是个人知识沉淀,一套结构清晰.体验优雅的文档系统都能显著提升 ...
- 解决nvm ls-remote 列表只出现iojs版本
前言 在 nvm 安装 node 时发现显示不存在此版本,使用 nvm ls-remote 查看可安装列表时发现,列表中只有 iojs $ nvm ls-remote iojs-v1.0.0 iojs ...
- WPS常用快捷键汇总
创建新文档 Ctrl+N或者Alt+F+N(对应baiNew) 打开文档 Ctrl+O或者Alt+F+O(对应Open) 关闭文du档 Ctrl+W或者Alt+W+C 保存当前文zhi档 Ctrl+S ...
- Hololens2 开发(仿真器)配置
博客地址:https://www.cnblogs.com/zylyehuo/ 参考链接 1.hololens 开发(仿真器)环境配置 2.visual studio 2019安装后添加工作负载 3.H ...
- 求水仙花数和敲桌子小游戏(C++版)
求水仙花数 for循环书写水仙花数 #include<iostream> using namespace std; int main() { int num = 0; int a=0, b ...
- ArrayBlockingQueue的take()底层原理
一.ArrayBlockingQueue 的 take() 方法的底层源码的详细介绍 ArrayBlockingQueue 是 Java 并发包 (java.util.concurrent) 中的一个 ...
- eolinker环境变量配置:用例执行前给把某参数设置为全局参数的方法
特别注意:需要使用全局变量或者预处理前务必阅读本链接https://www.cnblogs.com/becks/p/13713278.html 1.场景分析 注册会员流程共计有添加数据,校验数据,提交 ...
- DPDI(Dispatch PDI)kettle调度管理平台之实操演练第002讲-最强三件套之Dispatch PDI+PDI+PRD生成DPDI应用数据库数据字典
DPDI实操演练第002讲 最强三件套之Dispatch PDI+PDI+PRD生成DPDI应用数据库数据字典 1.案例适用范围 Dispatch PDI资源仓库管理使用可参考 Dispatch ...
- 访问项目resource/static目录下的模板文件(解决Docker部署后访问不到的问题)
使用ClassPathResource方式获取static下的文件(别的方式本地可以访问到,Docker部署后不行) final String templatePath = "/static ...