我是靠谱客的博主 缓慢大象,这篇文章主要介绍ACM-ICPC 2018 南京赛区网络预赛 签到水题,现在分享给大家,希望可以做个参考。

A An Olympian Math Problem

  •  1000ms
  •  65536K

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 n.

It is guaranteed that 2≤n≤10^18.

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.

样例输入

复制代码
1
2
3
2 2 3

样例输出

复制代码
1
2
1 2

题目来源

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

 

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

(n-1)*(n-1)!+(n-2)*(n-2)!=(n-2)!*(n-n-1)(mod n)=(n-2)!*(n-1)(注 -1%n=n-1)......

可得 S = n-1 

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdio> int main() { int t; long long n; scanf("%d",&t); while(t--){ scanf("%lld",&n); printf("%lldn",n-1); } return 0; }

 

最后

以上就是缓慢大象最近收集整理的关于ACM-ICPC 2018 南京赛区网络预赛 签到水题的全部内容,更多相关ACM-ICPC内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部