当前位置: 代码迷 >> 综合 >> USCAO-Section 1.4 Arithmetic Progressions
  详细解决方案

USCAO-Section 1.4 Arithmetic Progressions

热度:79   发布时间:2023-09-19 12:08:44.0

原题:
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 progressions of length n in the set S of bisquares. The set of bisquares is defined as the set of all integers of the form p2 + q2 (where p and q are non-negative integers).

题意:
求由0<=p,q<=M生成的(p^2+q^2)的集合中长度为N的等差数列a+bn的a和b并按照b优先,b相等按a排序的要求按顺序输出。

输入M,N,N是数列长度要求,M是pq的上限,用小于等于M的p和q生成集合。M小鱼250.

水题,直接上代码:

/*
ID:newyear111
PROG: ariprog
LANG: C++
*/#include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
using namespace std;
const int N=150000;
//node用来记录等差数列的first_number和公差 
struct Node{int a,b;
}node[N];int pq[N];//pq[i]=1代表 i是双平方集合中存在的值 
int T[N];//将上面的双平方数存入其中 
int n,m;
int lenAns,lenpq; 
//根据题目要求对结果进行排序 
bool cmp(Node x,Node y){return (x.b<y.b)||(x.b==y.b&&x.a<y.a);
} 
//生成从小到大排序好的双平方数集合 
void init(){for(int i=0;i<=m;i++){for(int j=i;j<=m;j++)pq[i*i+j*j]=1;}for(int i=0;i<N;i++){if(pq[i])T[lenpq++]=i;}
}
int main()
{ifstream fin("ariprog.in");ofstream fout("ariprog.out");//  计算出双平方数最大为多少 cout<<250*250*2<<endl; 125000在数组的可接受范围内,我们可以用数组的index进行标记 fin>>n>>m;init();//生成集合//遍历集合中的所有数  找出满足条件的等差数列 for(int i=0;i<lenpq;i++){for(int j=i+1;j<lenpq;j++){int b=T[j]-T[i];int a=T[i];int k;for(k=2;k<n;k++){if(!pq[a+b*k])break;}//记录满足条件是等差数列的头和公差 if(k==n){node[lenAns].a=a;node[lenAns].b=b;lenAns++;}}}sort(node,node+lenAns,cmp);if(!lenAns){fout<<"NONE"<<endl;}else{for(int i=0;i<lenAns;i++){fout<<node[i].a<<" "<<node[i].b<<endl;} }fin.close();fout.close();return 0;
}
  相关解决方案