题目链接: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. C#WinForm窗体事件执行次序

    当 Windows Form 应用程序启动时,会以下列顺序引发主要表单的启动事件:         System.Windows.Forms.Control.HandleCreated         ...

  2. Android测试跑单个包脚本文件

    脚本: adb shell monkey -p 应用包名 --throttle 随机事件间隔 -v -v -v -s 1 --ignore-security-exceptions --kill-pro ...

  3. 探究Visual Studio生成的.vs文件夹内部结构和作用

    https://shiyousan.com/post/636441130259624698 在某个契机的引发下,对VS解决方案中自动生成的.vs文件夹产生了兴趣,以前总对这个文件夹不怎么上心,最近正好 ...

  4. 关于Android打版本号的小白文

    尽管常常和android打交道.但事实上我对android不是非常了解. 这里记录一下ant编译androidproject的过程,然后顺便记录一下android的一些基本概念.不求渡人,但求渡己.这 ...

  5. 为什么GPL是更好的开源许可证?

    1. 让我从一件新闻讲起. 2009年,计算机业界发生了一件大事:甲骨文公司以74亿美元收购SUN公司. 消息宣布后,有一个人坚决反对这笔交易.他叫Michael Widenius,是数据库软件MyS ...

  6. 【代码审计】CLTPHP_v5.5.3后台任意文件删除漏洞分析

      0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chich ...

  7. 【RF库测试】算法运算

  8. Splash 简介与安装

    Splash 说白了就是一个轻量级的浏览器,利用它,我们同样可以实现跟其他浏览器一样的操作,我们使用 Docker 来安装 Splash: [root@localhost ~]# docker run ...

  9. 关于 Handler 与 opener

    我们可以使用 urllib.request.Request() 构造请求对象,但是对于一些更高级的操作,比如 Cookies 处理.代理设置 .身份验证等等,Request() 是处理不了的这时就需要 ...

  10. 目前学习.net时间让我摸不着头脑的事情

    呜呜,不太喜欢做笔记,只喜欢把自己不懂的和预习时间有麻烦的简单记下,所以这里也是能可以让我写下我们的学习点滴··· 今天学习了<1>.变量的作用域,在想是不是之前听过的局部变量和全局变量? ...