Arithmetic Progressions

链接

分析:首先预处理数据集,然后将数据集排序,我们对于当前位置,去搜索是否存在满足条件的等差数列即可,之前一直TLE,完全是map的锅,太慢了

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "map"
 5 #include "algorithm"
 6 #include "vector"
 7 using namespace std;
 8 const int maxn=2e5;
 9 struct Node{
10     int x,y;
11 };
12 int mp[maxn];
13 vector<int> h;
14 vector<Node> p;
15 int n,m,len;
16 bool cmp(Node a,Node b){
17     if(a.y==b.y){
18         return a.x<b.x;
19     }
20     return a.y<b.y;
21 }
22 int main()
23 {
24     cin>>n>>m;
25     for(int i=0;i<=m;i++){
26         for(int j=i;j<=m;j++){
27             int num=i*i+j*j;
28             if(!mp[num]){
29                 mp[num]=1;
30                 h.push_back(num);
31             }
32         }
33     }
34     sort(h.begin(),h.end());
35     len=h.size();
36     int cnt=0;
37     for(int i=0;i<len-n+1;i++){
38         for(int j=i+1;j<len-n+2;j++){
39             int cha=h[j]-h[i];
40             int flag=0;
41             if(h[i]+(n-1)*cha>h[len-1])  break;
42             for(int k=1;k<=n-2;k++){
43                 if(h[i]+(k+1)*cha>h[len-1]){
44                     flag=2; break;
45                 }
46                 if(!mp[h[j]+k*cha]){
47                     flag=1; break;
48                 }
49             }
50             if(flag==2)  break;
51             if(!flag){
52                 cnt++;
53                 Node tt;
54                 tt.x=h[i],tt.y=cha;
55                 p.push_back(tt);
56             }
57         }
58     }
59     if(cnt==0){
60         cout<<"NONE"<<endl;
61     }else{
62         sort(p.begin(),p.end(),cmp);
63         //cout<<cnt<<endl;
64         for(int i=0;i<p.size();i++)
65             cout<<p[i].x<<" "<<p[i].y<<endl;
66     }
67     return 0;
68 }

时间: 2024-08-16 17:33:04

Arithmetic Progressions的相关文章

poj 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theore

(素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a 

POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 素数 难度:0

http://poj.org/problem?id=3006 #include <cstdio> using namespace std; bool pm[1000002]; bool usd[1000002]; bool judge(int x) { if(usd[x])return pm[x]; usd[x] = true; if(x == 2) return pm[x] = true; if(((x & 1) == 0) || (x < 2))return pm[x] =

洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions

P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题解 最新讨论• 这道题有问题• 怎么进一步优化时间效率啊 …题目描述一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列.在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p的平方 + q的平

USACO 1.4 Arithmetic Progressions

Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer. Write a program that finds all arithmetic progression

poj3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16333   Accepted: 8205 Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing

【POJ3006】Dirichlet&#39;s Theorem on Arithmetic Progressions(素数筛法)

简单的暴力筛法就可. 1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cctype> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 using namespace std; 10 11 co

POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 快筛质数

题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么. 思路:这题主要考如何筛素数,线性筛.详见代码. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 1000010 using namespace std; int prime[MAX],primes; bool notp[MAX]; int a,d,n;

判断素数——Dirichlet&#39;s Theorem on Arithmetic Progressions

Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theore

Dirichlet&#39;s Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

题意 给出a d n    给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 bool Is_Primes[1000005]; 5 int Primes[1000005]; 6 int A[1000005]; 7 int cnt; 8 void Prime(int n){ 9 cnt=0; 1