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
题目来源
题解:
当时看到这个题,第一意识就是 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;
}