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 ...
随机推荐
- linux查看redis版本
1. redis-server --version 和 redis-server -v 2.redis-cli --version 和 redis-cli -v 如果报redis-server或red ...
- Delphi 非主窗体(即子窗体)在任务栏显示按钮
type TForm2 = class(TForm) private { Private declarations } public { Public declarations } procedure ...
- BUUCTF---bbbbbras
题目 p = 177077389675257695042507998165006460849 n = 3742182950988779627489716224936732940098864714561 ...
- 如何在 Git 书写良好的 Commit Messages
如何在 Git 书写良好的 Commit Messages Why(为什么编写) | How(如何编写) Why A diff will tell you what changed, but only ...
- 【SpringCloud】微服务架构编码构建
微服务架构编码构建 约定>配置>编码 Mysql的主从 slave会从master读取binlog来进行数据同步 三步骤+原理图 MySQL复制过程分成三步: master将改变记录到二进 ...
- 调用dll中form,太古老了,可是
太古老了,可是用的不多.应该考虑商品化项目首选. library Prj_dll; { Important note about DLL memory management: ShareMem mus ...
- ADM3251E使用一段时间后损坏
使用ADM3251E导致CPU发热 - 参考链接: https://bbs.elecfans.com/jishu_1687010_1_1.html 笔者设计的电路板在解决RS232隔离通信的时采用了A ...
- AQS的acquire(int arg) 方法底层源码
一.定义 acquire(int arg) 是 AQS(AbstractQueuedSynchronizer)中的一个核心方法,用于在独占模式下获取同步状态.如果当前线程无法获取同步状态,则将其加入等 ...
- kettle介绍-Step之REST Client
REST Client介绍 REST 客户端转换步骤可以消费 RESTful 服务.RESTful 是一种网络应用程序的设计风格和开发方式,基于 HTTP,可以使用 XML 格式定义或 JSON 格式 ...
- 异步编程与Tortoise-ORM框架
title: 异步编程与Tortoise-ORM框架 date: 2025/04/19 00:13:05 updated: 2025/04/19 00:13:05 author: cmdragon e ...