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 px em rem % vw vh vm 区别
前言 在传统项目开发中,我们只会用到 px.%.em 这几个单位长度,它们可以适用大部分项目的开发,并且拥有较好的兼容性. 而从 css3 开始,浏览器对逻辑单位的支持又提升了新的境界,增加了 rem ...
- centos操作collection
Centos修改IP地址 https://blog.csdn.net/weixin_45193791/article/details/124646170 Centos打开.修改.保存文件 https: ...
- burp suite使用(一) --- 抓包,截包,改包
接下来我将以一个新手的角度讲述如何使用burp来抓包,截包和改包. 我采用的是UC浏览器来配合burp的使用. 1.设置浏览器 设置---其他---更改代理设置 由于UC浏览器采用的是ie的内核,所以 ...
- DELPHI 检测服务器地址是否有效
利用DELPH 的ICMP控件检测服务器地址 function CheckNetServer():Boolean; begin IdIcmpClient1.Host := '192.168.1.230 ...
- Obsidian 笔记一键转换发布为 Jekyll 博客
Obsidian 是一款功能强大且灵活的知识管理和笔记软件,与 Jekyll 这一轻量级静态博客框架的结合,既能保留 Obsidian 的网状知识关联优势,又能借助 Jekyll 的高效编译能力快速生 ...
- Apollo批量给新创建的用户授可编辑权限
背景: 我们要在Apollo中批量给新创建的用户授可编辑权限 apollo系统版本: java-2.1.0 管理员账号:Apollo 可编辑账号:guoyabin 过程: 在没写这段代码的时候从网上搜 ...
- 《机器人SLAM导航核心技术与实战》第1季:第3章_OpenCV图像处理
<机器人SLAM导航核心技术与实战>第1季:第3章_OpenCV图像处理 视频讲解 [第1季]3.第3章_OpenCV图像处理-视频讲解 [第1季]3.1.第3章_OpenCV图像处理_认 ...
- java学习-8【EnumMap】
EnumMap和EnumSet几乎是一样的,区别时EnumMap的key时Enum. public enum Types { RED,GREEN,BLACK,YELLO } @Test public ...
- dxSpreadSheet的报表demo-关于设计报表模板问题
学习 dxSpreadSheetReportDesigner过程中发现: dxSpreadSheet通过dxSpreadSheetReportDesigner点击右键出现弹出菜单,自动生成如图的菜单和 ...
- Solon AI MCP Server 入门:Helloworld (支持 java8 到 java24。国产解决方案)
目前网上能看到的 MCP Server 基本上都是基于 Python 或者 nodejs ,虽然也有 Java 版本的 MCP SDK,但是鲜有基于 Java 开发的. 作为Java 开发中的国产顶级 ...