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).
TIME LIMIT: 5 secs
PROGRAM NAME: ariprog
INPUT FORMAT
Line 1: | N (3 <= N <= 25), the length of progressions for which to search |
Line 2: | M (1 <= M <= 250), an upper bound to limit the search to the bisquares with 0 <= p,q <= M. |
SAMPLE INPUT (file ariprog.in)
5 7
OUTPUT FORMAT
If no sequence is found, a singe line reading `NONE'. Otherwise, output one or more lines, each with two integers: the first element in a found sequence and the difference between consecutive elements in the same sequence. The lines should be ordered with smallest-difference sequences first and smallest starting number within those sequences first.
There will be no more than 10,000 sequences.
SAMPLE OUTPUT (file ariprog.out)
1 4 37 4 2 8 29 8 1 12 5 12 13 12 17 12 5 20 2 24
这题……擦!还敢更简单点么?我还敢更脑残一点么?到现在都不知道当时到底犯了什么脑残错。难道是最后的输出的num减了1?反正想死北平了我。‘
/*
ID: wtff0411
PROG: ariprog
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
int hash[999999];
struct r{
int s,d;
}res[10001];
bool cmp(r a,r b)
{
if(a.d<b.d)
return true;
if(a.d>b.d)
return false;
if(a.s<b.s)
return true;
return false;
}
int main()
{
freopen("ariprog.in","r",stdin);
freopen("ariprog.out","w",stdout);
int n,m;
int i,j,k=0;
int num=0;
cin>>n>>m;
memset(hash,0,sizeof(hash));
for(i=0;i<=m;i++)
for(j=i;j<=m;j++)
{hash[i*i+j*j]=1;
//hash2[k++]=i*i+j*j;
}
n--;
//for(i=0;i<=300;i++)
//{if(hash[i]==1)cout<<i<<endl;}
//sort(hash2,hash2+k-1)
for(i=m*m+m*(m);i>0;i--)
{
if(hash[i]==1)
{
int start=i;
for(j=i-1;j>0;j--)
{
if(hash[j]==1)
{
bool flag=0;
int second=j;
int dif=start-second;
int first=start-dif*n;
if((first)<0)
{//cout<<first<<endl;
break;}
for(k=1;k<n;k++)
{
if(hash[second-dif*k]!=1)
{flag=1;break;}
}
if(flag==0)
{
//cout<<first<<endl;
res[num].s=first;
res[num].d=dif;
num++;
}
}
}
}
}
sort(res,res+num,cmp);
for(i=0;i<num;i++)
cout<<res[i].s<<" "<<res[i].d<<endl;
if(num==0)
cout<<"NONE"<<endl;
//system("pause");
return 0;
}
简单直白题啊混蛋!需要注意的是读题读题读题读题!pq可以相等的你个呆瓜!hash开250*250就够了你个呆瓜!不要又过小了会超数组的啊呆瓜!去死吧呆瓜!
剪枝是参考了题解的。从后往前和先判断大于零。
……还有一题至今只过了两组数据,原因不明。不行了我快挂了。果然自己还是……