<cf>Square

B. Square
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).

John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?

The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17crosses.

Input

The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.

The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.

Output

For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use thecincout streams or the %I64d specifier.

Sample test(s)
input
3
4 8 100
output
17
33
401
思路:援引自http://codeforces.com/blog/entry/4673

Problem «Square»

Let the pencil move by the line and put crosses through every (n + 1) point. Lets associate every point on the line with point on square perimeter. Namely, point x on the line will be associated with point on square perimeter that we will reach if we move pencil around square to x clockwise. Then lower left corner will be associated with all points 4np for every non-negative integer p. Crosses will be associated with points k(n + 1) for some non-negative k. Nearest point of overlap of two families of points will be in point LCM(n + 14n). Then we will put  crosses.

AC code:

#include <iostream>
using namespace std;
int main()
{
    int T;
    long long n;
    cin>>T;
    while(T--)
    {
        cin>>n;
        long long a=4*n,b=n+1,r=a%b;
        while(r)
        {
            a=b;
            b=r;
            r=a%b;
        }
        long long cross=4*n/b+1;
        cout<<cross<<endl;
    }
    return 0;
}


posted on 2012-07-18 11:54  铁树银花  阅读(422)  评论(0编辑  收藏  举报

导航