计蒜客 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.
题意:
假设 $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题]的更多相关文章
- 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...
- 计蒜客 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 ...
- 计蒜客 31452 - Supreme Number - [简单数学][2018ICPC沈阳网络预赛K题]
题目链接:https://nanti.jisuanke.com/t/31452 A prime number (or a prime) is a natural number greater than ...
- 计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]
题目链接:https://nanti.jisuanke.com/t/31001 题意: 一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某 ...
- 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]
题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...
- 计蒜客 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 样例输入: ...
- 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]
题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...
- 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]
题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...
- 计蒜客 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 ...
随机推荐
- SpEL、PropertyPlaceholderConfigurer与@Value、#{}、${}
概念 SpEL:Spring EL表达式 PropertyPlaceholderConfigurer:即org.springframework.beans.factory.config.Propert ...
- Java -- 异常的捕获及处理 -- 自定义异常类
7.4 自定义异常类 定义异常类只需要继承Exception类即可. 例:自定义异常类 Class : MyException package limeThrowable._7_4; public c ...
- Eclipse cdt解决github导入的项目无法打开声明的bug (cannot open declaration)
概述: 我利用eclipse 的git插件clone github上的远程项目(C++)到本地时遇到一个问题:clone下来的项目没有C++特性,无法使用open declaration等操作,下面是 ...
- 有人在群里问关于SQL表组合数据问题
他的问题如下 如此我建表如下: 如果想根据用户进行分组后 又要显示所属门店在同一个字段中的话,这里需要用group_concat来显示 同时关联的时候可用find_in_set来处理 我设计的SQL如 ...
- ios开发之--textview意见反馈页面(占位label,字数统计,提交按钮的交互设置)
记录一个页面的功能: textview的占位符,字数统计,提交按钮的交互设置,具体效果图如下:
- Spring boot配置log4j输出日志
1. pom.xml文件中配置parent,版本选定[1.2.5.RELEASE] 关于为什么要选这个版本:我尝试使用[1.4.1.RELEASE],但该版本库里没有[spring-boot-star ...
- RF失败案例重跑
1.1 失败案例重跑 该功能主要是针对上次连跑失败的案例需要重新执行测试的情况,可自动识别上次执行失败的案例并进行重跑,无需手动选择相应的案例,简单高效. 1.5.1. 重 ...
- PyCharm 基础设置
设置主题:File -- Settings -- Editor -- Color & Fonts -- Font -- Scheme 设置为 Darcula 设置字体:File -- Sett ...
- 3dmax osg格式导出插件 osgExp OpenSceneGraph Max Exporter
https://sourceforge.net/projects/osgmaxexp/files/OpenSceneGraph%20Max%20Exporter/
- OBS显示器获取显示黑色没有图像