[usaco]1.4 Arithmetic Progressions

暴力搜索 + 簡單的剪枝

/*
ID:fuxiang2
PROG: ariprog
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <cmath>
 
#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
 
using namespace std;
ofstream fout ("ariprog.out");
ifstream fin ("ariprog.in");
 
int n,m;
//set<int> si;
 
int hash[62510 * 2];
 
class node
{
public:
    int a;
    int b;
 
    node(int _a, int _b){
        a = _a;
        b = _b;
    }
    bool operator < (const node & m )const {
        if(m.b == b) return a < m.a;
        return b < m.b;
    }
};
vector<node> vn;
 
//應該是這裏比較費時間
void work(int a,int b)
{
    int ans = a;
    if(hash[ans]  == 0)
        return ;
    FOR(i,0,n-1){
        ans += b;
        if(hash[ans] == 0)
            return ;
    }
    node t(a,b);
    vn.push_back(t);
 
}
int main()
{
    fin >> n >> m;
    FOR_1(i,0,m)
        FOR_1(j,0,m){
            int ii = i*i;
            int jj = j*j;
            hash[ii + jj] = 1;
        }
    int end = m*m ;
    FOR_1(i,0,end)
        FOR_1(j,1,end){
            if(  (i + (n-1)*j) <= 2*end)
                work(i,j);
            else
                break;
    }
    sort(vn.begin(),vn.end());
    if(vn.empty())
        fout << "NONE"<<endl;
    else
        for(vector<node>::iterator  iter =  vn.begin() ; iter != vn.end() ; iter ++ ){
            fout << iter->a << " "<< iter ->b<<endl;
        }
 
        return 0;
}

原始博客地址:http://www.fuxiang90.com/2012/07/usaco1-4-arithmetic-progressions/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章