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 5: TEST OK [0.016 secs, 11880 KB]
   Test 6: TEST OK [0.065 secs, 11880 KB]
   Test 7: TEST OK [0.378 secs, 11880 KB]
   Test 8: TEST OK [0.818 secs, 11880 KB]
   Test 9: TEST OK [0.802 secs, 11876 KB]

All tests OK.

Your program (‘ariprog‘) produced all correct answers! This is your submission #5 for this problem. Congratulations!

还是算蛮简单的一道构造题目= = 差点以为是要去搜索了...

/*
ID: wushuai2
PROG: ariprog
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int M = 660000         ;
const ll P = 10000000097ll   ;
const int INF = 0x3f3f3f3f   ;
const int MAX_N = 20         ;

int n, m;
bool status[M];
int a[M], len;
int llen;

struct sc{
    int a, b;
}ans[M];

bool cmp(struct sc a, struct sc b){
    if(a.b == b.b)  return a.a < b.a;
    return a.b < b.b;
}

void init(){
    int i, j;
    memset(a, 0, sizeof(a));
    len = 0;
    memset(status, 0, sizeof(status));
    for(i = 0; i <= m; ++i){
        for(j = 0; j <= m; ++j){
            int num = i * i + j * j;
            if(status[num]) continue;
            status[num] = true;
            a[len++] = num;
        }
    }
    a[len] = ‘\0‘;
    llen = 0;
}

int main() {
    ofstream fout ("ariprog.out");
    ifstream fin ("ariprog.in");
    int i, j, k, t, s, c, w, q;
    fin >> n >> m;
    init();
    sort(a, a + len);
    for(i = 0; i < len; ++i){
        int num = a[i];
        for(j = 0; j < len; ++j){
            int cur = a[j];     //fisrt a + b
            int b = cur - num;
            if(b * (n - 1) + num > a[len - 1])  break;
            if(b < 1)   continue;
            for(k = 2; k < n; ++k){
                if(!status[num + k * b]){
                    break;
                }
            }
            if(n == k){
                ans[llen].a = num;
                ans[llen++].b = b;
            }
        }

    }

    if(!llen){
        fout << "NONE" << endl;
        return 0;
    }
    sort(ans, ans + llen, cmp);
    for(i = 0; i < llen; ++i){
        cout << ans[i].a << ‘ ‘ << ans[i].b << endl;
        fout << ans[i].a << ‘ ‘ << ans[i].b << endl;
    }
    fin.close();
    fout.close();
    return 0;
}
时间: 2024-08-11 03:25:36

USACO Arithmetic Progressions 【构造等差数列】的相关文章

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

这道题目还是理解了很久的,英语烂真是头大. 题意是,求长度为N,且每个元素都是双平方数(双平方数是指一个数能被写成其他两个数的平方相加)的等比数列的个数. 给出长度N,和平方数的范围(两数q,p各小于M;也就是双平方数小于2*M*M) 暴搜+简单剪枝 /* ID: modengd1 PROG: ariprog LANG: C++ */ #include <iostream> #include <stdio.h> #include <queue> #include <

洛谷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的平

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

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

POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 快筛质数

题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么. 思路:这题主要考如何筛素数,线性筛.详见代码. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 1000010 using namespace std; int prime[MAX],primes; bool notp[MAX]; int a,d,n;

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