zoukankan      html  css  js  c++  java
  • <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;
    }
    


  • 相关阅读:
    龙年新作:水印文字添加工具源码摘要
    C语言关键字 浪里白条:goto
    继续聊WPF——自定义命令
    CSS3新的鼠标样式介绍
    C语言深入理解 常量与变量
    XCode 4 不能运行的解决办法
    Runtime专题:详解IOS开发应用之并发Dispatch Queues
    C语言关键字 乱世枭雄:static与extern
    一步步带你做vue后台管理框架(一)——介绍框架
    怎么在谷歌浏览器中安装.crx扩展名的离线Chrome插件?
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910510.html
Copyright © 2011-2022 走看看