Fake News(牛客暑假多校)

链接:https://ac.nowcoder.com/acm/contest/5672/D
来源:牛客网
Fake News
题目描述
McDonald Thumb, the greatest president ever in the history of the Great Tokitsukaze Kingdom has held his 100th press conference about the global pandemic after making his 1000000th tweets with his smartphone. With a big smile on his face, Mr. Thumb said that nobody knows math more than he does.

‘I learn math since I was born and I am very good at it’, said Mr. Thumb. ‘People keep asking me why I know math so much and I sometimes find myself having those amazing ideas about math.’

‘For example?’, asked by a reporter.

‘Oh you, I know you! Fake news! You and your agency always produce fake news!’, replied angrily by Mr. Thumb. ‘Let me teach you something, do you know if you add up the squares of numbers from 1 to n, the sum will never be a square number?’
As another reporter in that press conference, you also wondering that given n, whether ∑ nk=1 k2
is a square number. Specifically, we regard a positive number x as a square number when there exists an integer y satisfying y2=x

输入描述:
There are multiple test cases. The first line of the input contains an integer T ( 1 ≤ T ≤ 1 0 6 1 \leq T \leq 10^6 1T106)
indicating the number of test cases.

For each test case, the first line contains one integers n ( 1 ≤ n ≤ 1 0 15 1 \leq n \leq 10^{15} 1n1015).
输出描述:
If the sum is a square number, please output ‘Fake news!’ in one line. Otherwise, please output ‘Nobody knows it better than me!’ instead.
示例1
输入
复制
5
1
2
3
4
5
输出
复制
Fake news!
Nobody knows it better than me!
Nobody knows it better than me!
Nobody knows it better than me!
Nobody knows it better than me!
题意: 给定n判断 ∑ k = 1 n k 2 \sum_{k=1}^{n}k^2 k=1nk2是否是一个完全平方数,即判断12 +22 +32 +…+n2 是否是一个平方数
做法:

最简单的做法,直接特判n是否为1或24即可。证明比较复杂,可以参考:
https://www.zhihu.com/question/363661682

#include<bits/stdc++.h>
using namespace std;
long long t,n;
int main()
{
  scanf("%lld",&t);
  while(t--)
  {
      scanf("%lld",&n);
      if(n==1||n==24) printf("Fake news!\n");
      else printf("Nobody knows it better than me!\n");
  }
}

还有一种官方题解里很迷的做法:
n(n+1)(2n+1)/6,三个乘数分别先两两除下gcd,然后分别判定sqrt是否等于自己就好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葛济维的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值