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;
}