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).
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. |
5 7
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.
1 4 37 4 2 8 29 8 1 12 5 12 13 12 17 12 5 20 2 24
/* ID: XMzhou LANG: C++ TASK: ariprog */ #include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <utility> #include <cassert> using namespace std; #define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 #define mk make_pair #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 250 + 50; const int MAXS = 10000 + 50; const int sigma_size = 26; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; const int inf = 1 << 30; #define eps 1e-10 const long long MOD = 1000000000 + 7; const int mod = 10007; typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pii; typedef vector<int> vec; typedef vector<vec> mat; #define Bug(s) cout << "s = " << s << endl; ///#pragma comment(linker, "/STACK:102400000,102400000") int bisquares[2 * MAXN * MAXN]; int a[2 * MAXN *MAXN]; int n , m; vector <pair <int, int > > path; void creat_bisquares() { for(int i = 0; i <= m ; i++) { for(int j = i ; j <= m ; j++) { bisquares[i * i + j * j] = 1; } } } int main() { //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("ariprog.in","r",stdin); freopen("ariprog.out","w",stdout); #endif // Online_Judge while(~scanf("%d%d" , &n , &m)) { clr(bisquares , 0); creat_bisquares(); int num = 0; for(int i = 0 ; i <= 2 * m * m ; i++) { if(bisquares[i])a[num++] = i; } // for(int i = 0 ; i < num ; i++)cout << "a[i] = " << a[i] << endl; for(int i = 0 ; i < num ; i++) { for(int j = i + 1; j < num ; j++) { int d = a[j] - a[i]; if(a[i] + (n - 1) * d > 2 * m * m)break; int ok = 1; for(int k = 2 ; k < n ; k++) { if(!bisquares[a[i] + k * d]) { ok = 0; break; } } if(ok)path.push_back(mk(d , a[i])); } } sort(path.begin() , path.end()); if(path.size() == 0)puts("NONE"); else for(int i = 0 ; i < path.size() ; i++) { printf("%d %d\n" , path[i].second , path[i].first); } } return 0; }