ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem | (此题有毒)

题目:

Alice, a student of grade 6, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him. The problem is:

We denote k! :

k!=1×2×⋯×(k−1)×k

We denote S:

S=1×1!+2×2!+⋯+(n−1)×(n−1)!

Then S module n is ____________

You are given an integer n.

You have to calculate S modulo n.

Input

The first line contains an integer T(T≤1000), denoting the number of test cases.

For each test case, there is a line which has an integer nn.

It is guaranteed that 2≤n≤1e18.

Output

For each test case, print an integer S modulo n.

Hint

The first test is: S=1×1!=1, and 1 modulo 2 is 1.

The second test is: S=1×1!+2×2!=5 , and 5 modulo 3 is 2.

样例输入复制

2
2
3

样例输出复制

1
2

题目来源

ACM-ICPC 2018 南京赛区网络预赛


题解:

当时看到这个题,第一意识就是 1000阶乘,那longlong也绝对要爆炸,那就用数组依次按位存呗。

之后也是上面那个大概构思了下,S的式子也正好可以化为  n! -1,再%n,就可以推导出是n-1.

正当思路妥当,实施到一半时候,却一看提交,有的大佬竟然 2分钟就a了,而且这题通过率超高,大多都交了。

所以就开启了自闭模式。

 

结果后来才发现,这题真的是有毒,竟然结果是有规律的,打个表其实就好了,就仅仅 是输出 N-1就OK。(当场吐血)

对了要记得longlong(算是一个卡点吧)

PS :

记得之前接触过相关类似的,这次竟然又坑了,以后一定是要多长个心眼。

以后遇到这种类型,先打个表看看规律再说emm。

这次比赛没能好好打,还是要提升自己内功啊。#:< )


代码 :

(代码简单到都不想po出来)

#include<stdio.h>
int main(){
	long long int n;
	scanf("%lld",&n);
	while(n--){
		long long int m;
		scanf("%lld",&m);
		
		printf("%lld\n",m-1);
	}
	return 0;
	
}

 

全部评论

相关推荐

10-24 00:54
已编辑
门头沟学院 Java
牛客20646354...:这连小厂都找不到就离谱,只能说可能你根本没投什么小厂。说实话现在都要11月了,没什么岗位了。其实最好是在9月找,那时候暑假工刚走,岗位多的是,现在都占满了岗位了,秋招的秋招,顶替暑假工的也基本上都顶替了。 只能多投了,简历其实都差不多,你这都不是外卖+点评去找实习了,已经比好多人优秀了。实在找不到,可以降低一些标准的,能投到自研项目的小厂说实话可能比你去中大厂能学到更多东西。因为中大厂最多给你看一点点模块功能,小厂基本上全部代码甚至几个项目的代码都能拿到。
点赞 评论 收藏
分享
淬月星辉:专利是什么?至少描述一下吧,然后把什么计算机二级、普通话这种拉低格调的证书删掉,不然hr以为你没东西写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务