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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <map>
#include <algorithm>
#define MAX 125000

using namespace std;

int N,M;
bool flag;
int num[MAX];
int mm[MAX];
bool test(int start,int len);
int main()
{
    freopen("ariprog.in","r",stdin);
    freopen("ariprog.out","w",stdout);
    while( scanf("%d%d",&N,&M) != EOF ){
        int pos = 0;
        flag = false;
        memset(mm,0,sizeof(mm));
        for( int i = 0; i <= M; i++ ){
            for( int j = 0; j <= M; j++ ){
                int tmp = i*i+j*j;
                if( mm[tmp] != 1 ){
                    num[pos] = tmp;
                    mm[tmp] = 1;
                    pos++;
                }
            }
        }
        sort(num,num+pos);
        int up_B = (num[pos-1]-num[0])/(N-1);
        for( int i = 1; i <= up_B; i++ ){
            for( int j = 0; j < pos; j++ ){
                if( test(num[j],i) ){
                    flag = true;
                    printf("%d %d\n",num[j],i);
                }
                if( num[j]+(N-1)*i > num[pos-1] ){
                    break;
                }
            }
        }
        if( flag == false ){
            printf("NONE\n");
        }
    }
    return 0;
}

bool test(int start,int len){
    int tmp = start;
    for( int i = 0; i < N; i++ ){
        if( mm[tmp] != 1 ){
            return false;
        }
        tmp += len;
    }
    return true;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 22:18:42

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,且每个元素都是双平方数(双平方数是指一个数能被写成其他两个数的平方相加)的等比数列的个数. 给出长度N,和平方数的范围(两数q,p各小于M;也就是双平方数小于2*M*M) 暴搜+简单剪枝 /* ID: modengd1 PROG: ariprog LANG: C++ */ #include <iostream> #include <stdio.h> #include <queue> #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的平

【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

POJ - 3006 - Dirichlet&#39;s Theorem on Arithmetic Progressions = 水题

http://poj.org/problem?id=3006 给一个等差数列,求其中的第n个质数,答案保证不超过1e6.n还特别小?!!! 埃筛之后暴力. #include<algorithm> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<map> #include<set> #include<stack

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] =