USACO Arithmetic Progressions 【构造等差数列】

USER: Jeremy Wu [wushuai2]
TASK: ariprog
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.005 secs, 11880 KB]
   Test 2: TEST OK [0.008 secs, 11880 KB]
   Test 3: TEST OK [0.008 secs, 11876 KB]
   Test 4: TEST OK [0.014 secs, 11880 KB]
   Test 5: TEST OK [0.016 secs, 11880 KB]
   Test 6: TEST OK [0.065 secs, 11880 KB]
   Test 7: TEST OK [0.378 secs, 11880 KB]
   Test 8: TEST OK [0.818 secs, 11880 KB]
   Test 9: TEST OK [0.802 secs, 11876 KB]

All tests OK.

Your program ('ariprog') produced all correct answers! This is your submission #5 for this problem. Congratulations!

还是算蛮简单的一道构造题目= = 差点以为是要去搜索了...

/*
ID: wushuai2
PROG: ariprog
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int M = 660000         ;
const ll P = 10000000097ll   ;
const int INF = 0x3f3f3f3f   ;
const int MAX_N = 20         ;

int n, m;
bool status[M];
int a[M], len;
int llen;

struct sc{
    int a, b;
}ans[M];

bool cmp(struct sc a, struct sc b){
    if(a.b == b.b)  return a.a < b.a;
    return a.b < b.b;
}

void init(){
    int i, j;
    memset(a, 0, sizeof(a));
    len = 0;
    memset(status, 0, sizeof(status));
    for(i = 0; i <= m; ++i){
        for(j = 0; j <= m; ++j){
            int num = i * i + j * j;
            if(status[num]) continue;
            status[num] = true;
            a[len++] = num;
        }
    }
    a[len] = '';
    llen = 0;
}

int main() {
    ofstream fout ("ariprog.out");
    ifstream fin ("ariprog.in");
    int i, j, k, t, s, c, w, q;
    fin >> n >> m;
    init();
    sort(a, a + len);
    for(i = 0; i < len; ++i){
        int num = a[i];
        for(j = 0; j < len; ++j){
            int cur = a[j];     //fisrt a + b
            int b = cur - num;
            if(b * (n - 1) + num > a[len - 1])  break;
            if(b < 1)   continue;
            for(k = 2; k < n; ++k){
                if(!status[num + k * b]){
                    break;
                }
            }
            if(n == k){
                ans[llen].a = num;
                ans[llen++].b = b;
            }
        }

    }


    if(!llen){
        fout << "NONE" << endl;
        return 0;
    }
    sort(ans, ans + llen, cmp);
    for(i = 0; i < llen; ++i){
        cout << ans[i].a << ' ' << ans[i].b << endl;
        fout << ans[i].a << ' ' << ans[i].b << endl;
    }
    fin.close();
    fout.close();
    return 0;
}
原文地址:https://www.cnblogs.com/wushuaiyi/p/4261010.html