ACM-ICPC 2018 南京赛区网络预赛(蒟蒻刷题)

.A. An Olympian Math Problem

Alice, a student of grade 666, 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!k!:

k!=1×2×⋯×(k−1)×kk! = 1 \times 2 \times \cdots \times (k - 1) \times kk!=1×2××(k1)×k

We denote SSS:

S=1×1!+2×2!+⋯+S = 1 \times 1! + 2 \times 2! + \cdots +S=1×1!+2×2!++
(n−1)×(n−1)! (n - 1) \times (n-1)!(n1)×(n1)!

Then SSS module nnn is ____________

You are given an integer nnn.

You have to calculate SSS modulo nnn.

Input

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

扫描二维码关注公众号,回复: 6632677 查看本文章

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

It is guaranteed that 2≤n≤10182 \le n\le 10^{18}2n1018.

Output

For each test case, print an integer SSS modulo nnn.

Hint

The first test is: S=1×1!=1S = 1\times 1!= 1S=1×1!=1, and 111 modulo 222 is 111.

The second test is: S=1×1!+2×2!=5S = 1\times 1!+2 \times 2!= 5S=1×1!+2×2!=5 , and 555 modulo 333 is 222.

样例输入

2
2
3

样例输出

1
2

solution
打表发现为x-1
推了一会不知道为啥
CODE:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 
 5 int cal(int x,int p)
 6 {
 7     int ans = 1;
 8     for(int i=1;i<=x;i++)
 9     ans*=i,ans%=p;
10     return ans;
11 }
12 void work(int x)
13 {
14     int ans = 0;
15     for(int i=1;i<x;i++)
16     ans =(ans+i*cal(i,x))%x;
17     cout<<x<<" "<<ans<<endl;
18 }
19 signed main()
20 {
21   int T;
22   for(cin>>T;T;T--)
23   {
24     int x;cin>>x;
25     cout<<x-1<<endl;
26   }
27   return 0;
28     for(int i=1;i<=100;i++)
29     work(i);
30 }
 
 

猜你喜欢

转载自www.cnblogs.com/zhangbuang/p/11093542.html