码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-01 20:28:35      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:tin   tput   网络   alice   数学   begin   wan   http   南京   

题目链接: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.

 

题意:

假设 $S\left( n \right) = 1 \times 1! + 2 \times 2! + \cdots + \left( {n - 1} \right) \times \left( {n - 1} \right)!$,求 $S\left( n \right)$ 模 $n$ 的余数。

 

题解:

$\begin{array}{l} 1 + S\left( n \right) \\ = 1 + 1 \times 1! + 2 \times 2! + \cdots + \left( {n - 1} \right) \times \left( {n - 1} \right)! = 2 \times 1! + 2 \times 2! + \cdots + \left( {n - 1} \right) \times \left( {n - 1} \right)! \\ = 2! + 2 \times 2! + \cdots + \left( {n - 1} \right) \times \left( {n - 1} \right)! = 3 \times 2! + \cdots + \left( {n - 1} \right) \times \left( {n - 1} \right)! \\ = 3! + 3 \times 3! + \cdots + \left( {n - 1} \right) \times \left( {n - 1} \right)! = 4 \times 3! + \cdots + \left( {n - 1} \right) \times \left( {n - 1} \right)! \\ = \cdots = \left( {n - 1} \right)! + \left( {n - 1} \right) \times \left( {n - 1} \right)! = n \times \left( {n - 1} \right)! = n! \\ \end{array}$

所以有 $S\left( n \right)\bmod n = \left( {n! - 1} \right)\bmod n = \left( {n! + n - 1} \right)\bmod n = n!\bmod n + \left( {n - 1} \right)\bmod 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题]

标签:tin   tput   网络   alice   数学   begin   wan   http   南京   

原文地址:https://www.cnblogs.com/dilthey/p/9571298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!