ACM Training Training Day 3 of a question ...

Source: Internet
Author: User

A Arithmetic progression A. Title Description:

A arithmetic progression is one that can be represented as a, a+b, a+2b,..., a+nb (n=0,1,2,3,...) of the sequence.

In this problem A is a non-negative integer and B is a positive integer. Write a program to find out the set of double-squared numbers (the double-squared-number set is all a set of squares that can be represented as P's squared + Q, where p and Q are non-negative integers) s in length n of arithmetic progression.

two. Format

time limit : 5 Seconds

title name : Ariprog

Input Format :

(File ariprog.in)

The first line: N (3<= n<=25), the length of the arithmetic progression to find.

Second line: M (1<= m<=250), search for the upper bound of the double square number 0 <= p,q <= M.


output Format :

(File ariprog.out)

If no sequence is found, the output is "NONE".

If found, outputs one or more rows, each consisting of two integers: a, B. A is the first value of arithmetic progression, and B is the tolerance of arithmetic progression.

These lines should be sorted by B and then by a.

No more than 10,000 arithmetic progression will be asked for.

three.

Sample input:

5

7

Sample output:

1 4

37 4

2 8

29 8

1 12

5 12

13 12

17 12

5 20

2 24

Analysis:

For the limit time more lenient this question Yes ~~~~~ violent poor lift can be ~~~~~~

According to test instructions, first determine all the double square number, and then according to the upper limit, the range of double squares stored up.

(oops, lazy, don't want to write a =.=)

I have comments in my code ... You can add it later if you have free time. ╮(╯▽╰)╭

Don't say it, on the code.

1#include <iostream>2#include <cstring>3 using namespacestd;4 BOOL  is[125001];//a bool array that records who is a double-squared number5 inta[125001];//since the given limit is 250, the maximum number is 250*250*26 intMain () {//N is the length of the given arithmetic progression, top is the upper tolerance limit7     intn,top,k=0, tole,num=0;//K is initialized to 0, and the last is the total length of the stored double-squared array. 8scanf"%d%d", &n,&top);//Tole is a tolerance, Nu records the number of arrays that satisfy the condition, and is used to determine if a subsequent bool f=09      for(intI=0; i<=top;i++)//record whether all the numbers are double squaresTen          for(intj=0; j<=top;j++) One              is[i*i+j*j]=1;  A      for(intI=0; i<=top*top*2; i++)//store the double squares in the upper range -         if( is[i]) -A[k++]=i;//there are altogether K numbers the     intMaxt= (top*top*2-0)/(n1);//define the maximum range of tolerances to Tole,maxt to Num is the number of records arithmetic progression -      for(tole=1; tole<=maxt;tole++) {//l for Tolerance, A[i] -          for(intI=0; a[i]+ (n1) *tole<=top*top*2&&i<k;i++) {//a[i]+ (n-1) *l for subparagraph I -             BOOLf=1; +              for(intj=1; j<n&&f;j++)//check that for each arithmetic progression, each of its elements is in a double-squared array -                 if(! is[a[i]+j*Tole]) { +f=0; A                 } at             if(f) {//after each inspection of the arithmetic progression, F is still true, the test instructions is satisfied -printf"%d%d\n", a[i],tole);//a sequence of conditions that satisfies the first and the tolerance of the output -num++;//Num records the number of columns that meet the criteria -             } -         } -     } in     if(!num)//num is 0, the output is none -printf"NONE"); to     return 0; +}

ACM Training Training Day 3 of a question ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.