计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]

Posted dilthey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]相关的知识,希望对你有一定的参考价值。

题目链接:https://nanti.jisuanke.com/t/30990

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 * 3 * … * (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.

 

题意:

假设 $Sleft( n ight) = 1 imes 1! + 2 imes 2! + cdots + left( {n - 1} ight) imes left( {n - 1} ight)!$,求 $Sleft( n ight)$ 模 $n$ 的余数。

 

题解:

$egin{array}{l} 1 + Sleft( n ight) \ = 1 + 1 imes 1! + 2 imes 2! + cdots + left( {n - 1} ight) imes left( {n - 1} ight)! = 2 imes 1! + 2 imes 2! + cdots + left( {n - 1} ight) imes left( {n - 1} ight)! \ = 2! + 2 imes 2! + cdots + left( {n - 1} ight) imes left( {n - 1} ight)! = 3 imes 2! + cdots + left( {n - 1} ight) imes left( {n - 1} ight)! \ = 3! + 3 imes 3! + cdots + left( {n - 1} ight) imes left( {n - 1} ight)! = 4 imes 3! + cdots + left( {n - 1} ight) imes left( {n - 1} ight)! \ = cdots = left( {n - 1} ight)! + left( {n - 1} ight) imes left( {n - 1} ight)! = n imes left( {n - 1} ight)! = n! \ end{array}$

所以有 $Sleft( n ight)mod n = left( {n! - 1} ight)mod n = left( {n! + n - 1} ight)mod n = n!mod n + left( {n - 1} ight)mod n = n - 1$。

 

AC代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    long long n;
    while(t--)
    {
        cin>>n;
        cout<<n-1<<endl;
    }
}

 



以上是关于计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]的主要内容,如果未能解决你的问题,请参考以下文章

计蒜客课程数据结构(顺序表)

2017计蒜客(四,五,六)

计蒜客之矩阵翻转

计蒜客 - 质数原根

计蒜客 | 欧拉回图 | 判断欧拉回路

计蒜客(三角形的内点)