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 × ⋯ × (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 ≤ 1018.

Output

For each test case, print an integer S modulo n.

Hint

The first test is: S = 1 × 1! = 1, and 1 modulo 2 is 1.

The second test is: S = 1 × 1! + 2 × 2! = 5 , and 5 modulo 3 is 2.

样例输入

2
2
3

样例输出

1
2

题意:

已知S = 1 × 1! + 2 × 2! + ⋯ + (n−1) × (n−1)!,求S%n的值。

思路:

直接上结论吧,S%n=n-1

#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
cout<<n-<<endl;
}
return ;
}

【2018 ICPC南京网络赛 A】An Olympian Math Problem(数论题)的更多相关文章

  1. 2018 ICPC南京网络赛 L Magical Girl Haze 题解

    大致题意: 给定一个n个点m条边的图,在可以把路径上至多k条边的权值变为0的情况下,求S到T的最短路. 数据规模: N≤100000,M≤200000,K≤10 建一个立体的图,有k层,每一层是一份原 ...

  2. 2018 ICPC南京网络赛 Set(字典树 + 合并 + lazy更新)

    题解:n个集合,你要进行m个操作.总共有3种操作.第一种,合并两个集合x和y.第二张,把特定的集合里面所有的数字加一.第三种,询问在某个集合里面,对于所有数字对2的k次方取模后,有多少个数字等于x. ...

  3. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  4. 2018 ICPC 徐州网络赛

    2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...

  5. 2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat

    题意:四个操作,区间加,区间每个数乘,区间的数变成 2^64-1-x,求区间和. 题解:2^64-1-x=(2^64-1)-x 因为模数为2^64,-x%2^64=-1*x%2^64 由负数取模的性质 ...

  6. 2013 ACM/ICPC 南京网络赛F题

    题意:给出一个4×4的点阵,连接相邻点可以构成一个九宫格,每个小格边长为1.从没有边的点阵开始,两人轮流向点阵中加边,如果加入的边构成了新的边长为1的小正方形,则加边的人得分.构成几个得几分,最终完成 ...

  7. 2018 icpc 青岛网络赛 J.Press the Button

    Press the Button Time Limit: 1 Second      Memory Limit: 131072 KB BaoBao and DreamGrid are playing ...

  8. 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)

    BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...

  9. 2018 icpc 徐州网络赛 F Features Track

    这个题,我也没想过我这样直接就过了 #include<bits/stdc++.h> using namespace std; ; typedef pair<int,int> p ...

随机推荐

  1. Web前端面试指导(九):盒子模型你是怎么理解的?

    问题分析 这道题问得比较宽泛,一定要找准切入点,如果切入点找不准,很容易乱答,甚至答偏,所以找准切入点是非常的重要的. 解答思路 1)盒子模型有两种,W3C和IE盒子模型 (1)W3C定义的盒子模型包 ...

  2. easyui datagrid 本地json数据 实现删除

    html代码:<a href='javascript:void(0);' onclick='Delete(\""+ index +"\")' class= ...

  3. json中定义数组

    我们经常会看到在js中定义普通函数: function f1(a,b){ console.log(a+b); } f1(); 今天我们看一下如何在json里边定义函数并调用: var json = { ...

  4. MySQL查询笔试综合题练习

    题目要求: 在某个数据库下建表: create table stu( -> name char(3) not null default '', -> subject varchar(10) ...

  5. 菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析

    一.概述. SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到. 二.代码演示. 需要添加的jar包. 2.1 we ...

  6. AOP的实现

    AOP基于xml配置方式实现 Spring基于xml开发AOP 定义目标类(接口及实现类) /** * 目标类 */ public interface UserService { //业务方法 pub ...

  7. php连接MySQL分析

    Mysql:在PHP脚本中操作MySQL数据库的的几个步骤如下: 1.连接MySQL数据库服务器,并判断是否连接正确 2.选择数据库,并设置字符集(可选) 3.执行SQL命令 4.处理结果集 5.关闭 ...

  8. python入门7 字符串操作

    字符串操作 #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 字符串操作 ""&quo ...

  9. 在VMware上安装ubuntu——网络类型

    安装虚拟机时,向导提示选择网络类型: 当使用仅主机模式网络时,虚拟机和物理机不能互相访问共享.

  10. Java Android 32位16位 MD5加密

    // md5加密 32位小写 private String Md5(String sourceStr) { String result = ""; try { MessageDig ...