当前位置:网站首页>2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
2022-04-23 03:23:00 【One more line and go to bed】
In a given interval [m,n] Inside , Is there a prime p、q、r(p<q<r), bring pq+r、qr+p、rp+q Are prime numbers ?
Input format :
Enter the two endpoints of the given interval 0<m<n≤1000, Separated by spaces .
Output format :
Output the number of prime triples that meet the conditions in one line .
sample input :
1 35
sample output :
10
Example interpretation
Satisfied 10 Group solution is :
2, 3, 5
2, 3, 7
2, 3, 13
2, 3, 17
2, 5, 7
2, 5, 13
2, 5, 19
2, 5, 31
2, 7, 23
2, 13, 17
Good question ! I didn't notice the key pruning during the simulation , But it's over . Explain the idea in detail
First 1000 The two largest primes within are 991 and 997, Is almost 10^6 The magnitude of , therefore , You need to make a table of prime numbers in advance .
To improve query speed , Add an additional array to use as a fast table . The author uses the prime number sieve + Square root discrimination .
Then it's traversal , Here's a key pruning : If it is three odd numbers, participate in the ternary operation given in the question , The result is an even number , Must not be prime . because 2 Is the only prime number , therefore , First determine whether the scope includes 2, In existence 2 Under the premise of , Just double cycle .
#include <bits/stdc++.h>
using namespace std;
int v[1000000]={0}; // Prime fast table
int main(void){
int m,n,sum=0;
vector<int> p; // Prime table
vector<int> d; // Check the sequence
p.push_back(2);
cin>>m>>n;
for(int i=3;i<1000000;i+=2){
int flg=1;
for(int j=0;j<p.size();j++){
if(i<p[j]*p[j])break;
if(!(i%p[j])){
flg=0;
break;
}
}
if(flg){
v[i]=1;
p.push_back(i);
}
}
for(int i=0;i<p.size();i++){
if(p[i]>n)break;
if(p[i]>=m)d.push_back(p[i]);
}
if(d[0]==2) // prune
for(int i=1;i<d.size()-1;i++){
for(int j=i+1;j<d.size();j++){
int a=d[i],b=d[j];
if(v[a*b+2]&&v[a*2+b]&&v[b*2+a])sum++;
}
}
cout<<sum;
return 0;
}

版权声明
本文为[One more line and go to bed]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230322232915.html
边栏推荐
- C set
- 超好用的【通用Excel导入功能】
- Detailed explanation of socket programming send() and recv() functions
- Query stored procedures in PostgreSQL
- New ORM framework -- Introduction to beetlsql
- 你真的懂hashCode和equals吗???
- Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
- 12.<tag-链表和常考点综合>-lt.234-回文链表
- Explanation keyword of MySQL
- 2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
猜你喜欢

Quartz. Www. 18fu Used in net core

研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台

L3-011 直捣黄龙 (30 分)
![Use of ADB command [1]](/img/e6/fb8fd6956c226f75b831f8eb46277f.png)
Use of ADB command [1]

Visual programming -- how to customize the mouse cursor
![General testing technology [1] classification of testing](/img/f1/d80b6793b6443cbc4048d7e6319f51.png)
General testing technology [1] classification of testing

New ORM framework -- Introduction to beetlsql

2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
![Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]](/img/1a/a22b4a35d3c083438d0f766a5ecb08.png)
Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]

【VS Code】解决jupyter文件在vs code中显示异常的问题
随机推荐
MySQL keyword group_ Concat, combined connection query
Mysql database design specification
Super easy to use [general excel import function]
Flink customizes the application of sink side sinkfunction
Configuration table and page information automatically generate curd operation page
Idea view history [file history and project history]
Course design of Database Principle -- material distribution management system
EasyUI's combobox implements three-level query
全新的ORM框架——BeetlSQL介绍
Log4net is in Net core usage
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
Experiment 6 input / output stream
Knowledge of software testing~
Flink real-time data warehouse project - Design and implementation of DWS layer
Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
Eight elder brothers chronicle [4]
Query stored procedures in PostgreSQL
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
General test technology [II] test method
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)