2018ICPC南京赛区网络预赛A: An Olympian Math Problem

Alice, a student of grade 66, 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 SS:

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 .

样例输入

2
2
3

样例输出

1
2

签到题一道,求出 S 然后再求模 n ,主要就是一个思维转换的问题,找出规律即可。
其中两点:
1: S 简化后的结果就是 n ! 1 ;
2: n ! 1 取模 n 之后的结果为 n 1 .

代码:

#include<iostream>
#include<cstdio>
#include<cstring> 
#include<algorithm>
#include<cmath>
#include<vector>
#include<deque>
#include<queue>
#include<ctime>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;

int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        LL k;
        scanf("%lld",&k);
        printf("%lld\n",k-1);
    }
}

猜你喜欢

转载自blog.csdn.net/qinying001/article/details/82313720
今日推荐