编程介的小学生 2020-09-28 09:03 采纳率: 0.4%
浏览 17

Become A Hero 具体用什么方式实现呢

Problem Description
Lemon wants to be a hero since he was a child. Recently he is reading a book called “Where Is Hero From” written by ZTY. After reading the book, Lemon sends a letter to ZTY. Soon he recieves a reply.

Dear Lemon,
It is my way of success. Please caculate the algorithm, and secret is behind the answer. The algorithm follows:
Int Answer(Int n)
{
.......Count = 0;
.......For (I = 1; I <= n; I++)
.......{
..............If (LCM(I, n) < n * I)
....................Count++;
.......}
.......Return Count;
}
The LCM(m, n) is the lowest common multiple of m and n.
It is easy for you, isn’t it.
Please hurry up!
ZTY

What a good chance to be a hero. Lemon can not wait any longer. Please help Lemon get the answer as soon as possible.

Input
First line contains an integer T(1 <= T <= 1000000) indicates the number of test case. Then T line follows, each line contains an integer n (1 <= n <= 2000000).

Output
For each data print one line, the Answer(n).

Sample Input
1
1

Sample Output
0

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-25 09:22
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # Calculate the Least Common Multiple (LCM)
    lcm <- function(a, b){
        return(abs(a*b)/gcd(a,b))
    }
    
    # Calculate the Greatest Common Divisor (GCD)
    gcd <- function(a, b){
        while(b != 0){
            temp <- b
            b <- a % b
            a <- temp
        }
        return(a)
    }
    
    # Main function
    answer <- function(n){
        count <- 0
        for(i in 1:n){
            if(lcm(i,n)<n*i){count += 1}
        }
        return(count)
    }
    
    # Test cases
    t <- 5 # Number of test cases
    for(i in 1:t){
        n <- readline() # Read input from stdin
        print(answer(n)) # Print output to stdout
    }
    
    评论

报告相同问题?