USACO-Section1.4 airprog[枚举]

来源:互联网 发布:记账软件破解版 编辑:程序博客网 时间:2024/05/22 17:04

题目大意:

一个等差数列是一个能表示成a, a+b, a+2b,…, a+nb (n=0,1,2,3,…)的数列。

在这个问题中a是一个非负的整数,b是正整数。写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p的平方 + q的平方的数的集合,其中p和q为非负整数)S中长度为n的等差数列。

样例输入:

5
7

样例输出:

1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24

题解:

先把可能的情况全部存下来,在用两次循环暴力搜索所有可能性,枚举所有情况。注意范围上限。

C++/*ID: mujinui1PROG: ariprogLANG: C++*/#include<stdio.h>#include<iostream>#include<fstream>using namespace std;int ma=0;int main() {    ifstream fin("ariprog.in");    ofstream fout("ariprog.out");    int n,m,count=0;    fin>>n>>m;    int a[70000];    for(int p=0;p<=m;p++){        for(int q=0;q<=m;q++){            a[count++]=p*p+q*q;        }    }//  for(int i=0;i<count;i++){//      cout<<i<<" "<<a[i]<<endl;//  }for(int i=0;i<count-1;i++){    for(int j=i;j<count;j++){        if(a[i]>a[j]){            int temp;            temp=a[i];            a[i]=a[j];            a[j]=temp;        }    }}for(int i=1;i<=m*m;i++){    for(int j=0;j<=m*m;j++){        if(j+(n-1)*i>a[count-1]){            break;        }        else{            int flag=0;            for(int len=0;len<n;len++){              for(int k=0;k<count;k++){                if(a[k]==j+i*len){                    flag++;                    break;                }              }            if(flag==n){                ma++;                fout<<j<<" "<<i<<endl;             }           }            }    }}if(ma==0){    fout<<"NONE"<<endl;}    return 0;}