usaco Arithmetic Progressions

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

题意是,求长度为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;
}

  

时间: 2024-08-01 12:26:02

usaco Arithmetic Progressions的相关文章

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

USACO Arithmetic Progressions(暴力)

题目请点我 题解: 这道题的题意是找出集合里所有固定长度为N的等差数列,集合内的元素均为P^2+q^2的形式(0<=p,q<=M).时间要求5s内,本着KISS,直接暴力.但是后来竟超时了,检查后发现是map的问题,本想利用map实现常数级的查找,但是显然map内部不是这样的,所以对于普通的数据类型,数据量不大(250^2+250^2)的情况下还是利用数组标记查找好一点,get. 代码实现: /* ID: eashion LANG: C++ TASK: ariprog */ #include

USACO 1.4 Arithmetic Progressions

Arithmetic Progressions 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 progression

洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions

P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题解 最新讨论• 这道题有问题• 怎么进一步优化时间效率啊 …题目描述一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列.在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p的平方 + q的平

poj 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theore

(素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a 

POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 素数 难度:0

http://poj.org/problem?id=3006 #include <cstdio> using namespace std; bool pm[1000002]; bool usd[1000002]; bool judge(int x) { if(usd[x])return pm[x]; usd[x] = true; if(x == 2) return pm[x] = true; if(((x & 1) == 0) || (x < 2))return pm[x] =

poj3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16333   Accepted: 8205 Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing

【POJ3006】Dirichlet&#39;s Theorem on Arithmetic Progressions(素数筛法)

简单的暴力筛法就可. 1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cctype> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 using namespace std; 10 11 co