码迷,mamicode.com
首页 > 其他好文 > 详细

usaco Arithmetic Progressions

时间:2015-08-28 16:57:20      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

这道题目还是理解了很久的,英语烂真是头大。

题意是,求长度为N,且每个元素都是双平方数(双平方数是指一个数能被写成其他两个数的平方相加)的等比数列的个数。

给出长度N,和平方数的范围(两数q,p各小于M;也就是双平方数小于2*M*M)

暴搜+简单剪枝

/*
ID: modengd1
PROG: ariprog
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <memory.h>
using namespace std;
bool isBis[2*251*251];
int N,M;
struct node
{
    int a,b;
    node(int aa,int bb)
    {
        a=aa;
        b=bb;
    }
    node(){}
    bool friend operator<(node n1,node n2)
    {
        if(n1.b==n2.b)
            return n1.a>n2.a;
        else
            return n1.b>n2.b;
    }
};
void Init()
{
    memset(isBis,false,sizeof(isBis));
    for(int i=0;i<=M;i++)
    {
        for(int j=0;j<=M;j++)
        {
            isBis[i*i+j*j]=true;
        }
    }
}
bool islegal(int a,int b)
{
    if(!isBis[a+b*(N-1)])
        return false;
    for(int i=0;i<N;i++)
    {
        if(!isBis[a])
            return false;
        a+=b;
    }
    return true;
}
int main()
{
    freopen("ariprog.in","r",stdin);
    freopen("ariprog.out","w",stdout);
    int ans=0;
    priority_queue<node> Q;
    scanf("%d",&N);
    scanf("%d",&M);
    Init();
    for(int i=0;i<=M*M;i++)
    {
        for(int j=1;j<=M*M;j++)
        {
            if((i+(N-1)*j)>2*M*M)//简单剪枝
                break;
            if(islegal(i,j))
            {
                ans++;
                Q.push(node(i,j));
            }
        }
    }
    if(Q.empty())
        cout<<"NONE"<<endl;
    else
        while(!Q.empty())
        {
            node now=Q.top();
            Q.pop();
            cout<<now.a<<‘ ‘<<now.b<<endl;
        }
    return 0;
}

  

usaco Arithmetic Progressions

标签:

原文地址:http://www.cnblogs.com/modengdubai/p/4766635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!