题目链接: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-<<endl;
}
}

计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]的更多相关文章

  1. 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...

  2. 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)

    A. An Olympian Math Problem 54.28% 1000ms 65536K   Alice, a student of grade 66, is thinking about a ...

  3. 计蒜客 31452 - Supreme Number - [简单数学][2018ICPC沈阳网络预赛K题]

    题目链接:https://nanti.jisuanke.com/t/31452 A prime number (or a prime) is a natural number greater than ...

  4. 计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]

    题目链接:https://nanti.jisuanke.com/t/31001 题意: 一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某 ...

  5. 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...

  6. 计蒜客 30994 - AC Challenge - [状压DP][2018ICPC南京网络预赛E题]

    题目链接:https://nanti.jisuanke.com/t/30994 样例输入: 5 5 6 0 4 5 1 1 3 4 1 2 2 3 1 3 1 2 1 4 样例输出: 55 样例输入: ...

  7. 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]

    题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...

  8. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

  9. 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]

    题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ...

随机推荐

  1. UpLoader------实现上传大文件

    代码: <div id="selectFile">选择文件1</div> <script> var da = newGuid(); var kk ...

  2. Java求解汉诺塔问题

    汉诺塔问题的描述如下:有3根柱子A.B和C,在A上从上往下按照从小到大的顺序放着一些圆盘,以B为中介,把盘子全部移动到C上.移动过程中,要求任意盘子的下面要么没有盘子,要么只能有比它大的盘子.编程实现 ...

  3. 开启apache的server-status辅助分析工具

    在Apache的调优过程中,可以通过查看Apache提供的server-status(状态报告)来验证当前所设置数值是否合理,在httpd.conf文件中做如下设置来打开: #加载mod_status ...

  4. 记安装ubuntu server和一些程序

    1. 安装ubuntu server 按照流程走了一遍,一切画面都正常,就是重启后界面只有一个光标闪啊闪,我不知道应该再装一遍还是找老大来解决,想了下,以前电脑就是因为出错才重装的,现在这个情况有可能 ...

  5. MyEclipse weblogic Deploy Location项目名称不正确解决方案

    MyEclipse weblogic Deploy Location项目名称不正确 MyEclipse部署weblogic 项目,名称错误,是别的项目名称 ====================== ...

  6. Kafka producer介绍

    Kafka 0.9版本正式使用Java版本的producer替换了原Scala版本的producer.本文着重讨论新版本producer的设计原理以及基本的使用方法. 新版本Producer 首先明确 ...

  7. Bugly最简单的配置方法

    1,注册Bugly帐号 2.项目 build.gradle 中配置 compile 'com.tencent.bugly:crashreport:latest.release'//其中latest.r ...

  8. struts1的配置文件详解

    要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...

  9. Objective-C官方文档 协议

    版权声明:原创作品,谢绝转载!否则将追究法律责任. 在现实生活中,当处理某一情况的时候人们往往遵循严格的程序.执法人员他们在打官司的收集证据和询问的时候一定要遵守协议. 在面向对象的语言中,最重要的是 ...

  10. Microsoft 设计原则

    在本文中 关于现代设计 技术为本 实现以较少投入取得极大成绩 迅速和流畅 真正实现数字化 合作共赢 相关主题 驱动出色设计的基础 我们相信遵循 Microsoft 设计原则可帮助你构建使用户感到愉悦并 ...