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;
}
}

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

计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)的更多相关文章

  1. 计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]

    题目链接:https://nanti.jisuanke.com/t/30990 Alice, a student of grade 6, is thinking about an Olympian M ...

  2. 计蒜客 30999.Sum-筛无平方因数的数 (ACM-ICPC 2018 南京赛区网络预赛 J)

    J. Sum 26.87% 1000ms 512000K   A square-free integer is an integer which is indivisible by any squar ...

  3. 计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)

    G. Lpl and Energy-saving Lamps 42.07% 1000ms 65536K   During tea-drinking, princess, amongst other t ...

  4. 【ACM-ICPC 2018 南京赛区网络预赛 A】An Olympian Math Problem

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 估计试几个就会发现答案总是n-1吧. 队友给的证明 [代码] #include <bits/stdc++.h> #def ...

  5. 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)

    H.Ryuji doesn't want to study 27.34% 1000ms 262144K   Ryuji is not a good student, and he doesn't wa ...

  6. 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)

    若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...

  7. 【计蒜客】是男人就过 8 题--Pony.AI 题 A. A String Game 后缀自动机+SG函数

    [题目]A. A String Game [题意]给定目标串S和n个子串Ti,Alice和Bob轮流选择一个子串操作,必须且只能在子串末尾添加一个字符使得新串也是S的子串,不能操作即输,求胜利者.|S ...

  8. 计蒜客 31458.Features Track-滚动数组+STL(map)连续计数 (ACM-ICPC 2018 徐州赛区网络预赛 F)

    F. Features Track Morgana is learning computer vision, and he likes cats, too. One day he wants to f ...

  9. 线段树+lazy标记 2019年8月10日计蒜客联盟周赛 C.小A的题

    题目链接:https://nanti.jisuanke.com/t/40852 题意:给定一个01串s,进行m次操作,|s|<=1e6,m<=5e5 操作有两种 l r 0,区间[l,r] ...

随机推荐

  1. [C/C++] #ifdef和#endif

    一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是“条件编译”.有时,希望当满足某条件时对一组语句进行编译,而当条 ...

  2. 用JavaScript写一个类似PHP print_r的函数

    PHP print_r的函数很好用,可以用来打印数组.对象等的结构与数据,可惜JavaScript并没有原生提供类似的函数.不过我们可以试着自己来实现这个函数,下面提供一些方法与思路. 方法一 fun ...

  3. How do I see what character set a database / table / column is in MySQL?

    Q: How do I see what the character set that a MySQL database, table and column are in? Is there some ...

  4. ACM模板~求逆序对的个数

    #include <map> #include <set> #include <cmath> #include <ctime> #include < ...

  5. hive连接数

    使用hive分析日志作业很多的时候,需要修改mysql的默认连接数 修改方法   打开/etc/my.cnf文件 在[mysqld]  中添加 max_connections=1000 重启mysql ...

  6. Every Programmer Should Know These Latency Numbers

    Every Programmer Should Know These Latency Numbers 1秒=1000毫秒(ms) 1秒=1,000,000 微秒(μs) 1秒=1,000,000,00 ...

  7. video视频在结束之后回到初始状态

    目前尝试了两种解决方案,但是方案1在安卓移动端无法生效(猜测是因为移动端安卓启动的是原生的视频播放控件的原因) 方案一: 重新load资源,这种方法比较简洁,但是在安卓下不适用 video.addEv ...

  8. leetcode-501. Find Mode in Binary Search Tree

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  9. 【BZOJ2338】【HNOI2011】数矩形 [计算几何]

    数矩形 Time Limit: 20 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 最近某歌手在研究自己的全国巡回演出, ...

  10. 「6月雅礼集训 2017 Day1」说无可说

    [题目大意] 给出n个字符串,求有多少组字符串之间编辑距离为1~8. n<=200,∑|S| <= 10^6 [题解] 首先找编辑距离有一个n^2的dp,由于发现只找小于等于8的,所以搜旁 ...