A. An Olympian Math Problem

  • 54.28%
  • 1000ms
  • 65536K
 

Alice, a student of grade 66, 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!:

k! = 1 \times 2 \times \cdots \times (k - 1) \times kk!=1×2×⋯×(k−1)×k

We denote SS:

S = 1 \times 1! + 2 \times 2! + \cdots +S=1×1!+2×2!+⋯+
(n - 1) \times (n-1)!(n−1)×(n−1)!

Then SS module nn is ____________

You are given an integer nn.

You have to calculate SS modulo nn.

Input

The first line contains an integer T(T \le 1000)T(T≤1000), denoting the number of test cases.

For each test case, there is a line which has an integer nn.

It is guaranteed that 2 \le n\le 10^{18}2≤n≤1018.

Output

For each test case, print an integer SS modulo nn.

Hint

The first test is: S = 1\times 1!= 1S=1×1!=1, and 11 modulo 22 is 11.

The second test is: S = 1\times 1!+2 \times 2!= 5S=1×1!+2×2!=5 , and 55 modulo 33 is 22.

样例输入复制

2
2
3

样例输出复制

1
2

题目来源

ACM-ICPC 2018 南京赛区网络预赛

题意很好理解。

直接代码

代码:

 //A-数学公式
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//公式为1*1!+2*2!+3*3!+...+n*n!=(n+1)!-1,本题为(n!-1)%n
//因为n!-1=(n-1)*n-1=(n-2)*n+n-1,所以[(n-2)*n+(n-1)]%n=n-1
//因n*n!=(n+1-1)n!=(n+1)n!-n!=(n+1)!-n!
//所以:1*1!=2!-1!
//2*2!=3!-2!
//3*3!=4!-3!
//.
//n*n!=(n+1)!-n!
//相加后有:1*1!+2*2!+3*3!+.+n*n!=(n+1)!-1
//1*1!+2*2!+3*3!+.+n*n!=(n+1)!-1
//把最后一项拆开来,变成(n+1-1)n!=(n+1)n!-n! int main()
{
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
cout<<n-<<endl;
}
}

溜了,一会贴一下线段树的题目。

最新文章

  1. MVC中得到成员元数据的Description特性描述信息公用方法
  2. XlFileFormat Enumeration
  3. OnNcCalcSize改变标题栏等的高度
  4. 无线AP和无线路由器区别
  5. c++写入txt文件
  6. 什么是html,什么是php
  7. PHP学习笔记十八【构造函数】
  8. 爬取软考试题系列之ip自动代理
  9. .net core Entity Framework 与 EF Core
  10. Nginx 返回响应过滤响应内容
  11. ObjectArx2013新建工程出错的解决办法
  12. liunx存储管理之基础知识
  13. centos查看系统版本信息
  14. session失效时间设置
  15. Guava的RateLimiter在单机限流中的正确用法
  16. python GIL 全局锁,多核cpu下的多线程性能究竟如何?
  17. Dictionary,hashtable, stl:map有什么异同?
  18. Docker 图形化页面管理工具使用
  19. iis6.0 default web site 无法启动
  20. 专访msup创始人兼CEO刘付强:追求卓越是数据时代下企业和个人的生存之道

热门文章

  1. JAVA调用Oracle存储过程和函数
  2. BZOJ1305 [CQOI2009]dance跳舞 【网络流】
  3. [hdu 1398]简单dp
  4. Codeforces Round #531 (Div. 3) ABCDEF题解
  5. 带依赖包的maven打包配置
  6. node搭建文件服务器
  7. 在Debian9安装node和npm
  8. 完美兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面js代码
  9. eclipse怎样快速的给代码段添加try catch
  10. AtCoder Regular Contest 082 F