• <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;
    }
    


  • 相关阅读:
    RUNOOB.COM-python网络编程-(python3.5.0)
    windows查看服务
    计算机网络里的一些理解
    如果面试有傻逼问道oracle怎么启动的
    推荐一个学习数据库的地方
    电脑中的驱动程序是什么,是干什么的
    Raspberry Pi 4B 之 Python开发
    Ubuntu20.04+EdgexFoundry边缘计算微服务搭建-----遇到的问题-----make build 被墙问题
    Raspberry Pi 4B + Ubuntu 20.04 server for arm64 的wifi配置
    关于PicoNeo开发环境的Unity3D+AndroidSDK配置
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910510.html
Copyright © 2020-2023  润新知