当前位置: 代码迷 >> 综合 >> ACM-ICPC 2018 南京赛区网络预赛 A An Olympian Math Problem (简单数论题)
  详细解决方案

ACM-ICPC 2018 南京赛区网络预赛 A An Olympian Math Problem (简单数论题)

热度:14   发布时间:2023-11-15 16:12:03.0

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

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

We denote SSS:

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

Then SSS module nnn is ____________

You are given an integer nnn.

You have to calculate SSS modulo nnn.

Input

The first line contains an integer T(T≤1000)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 nnn.

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

Output

For each test case, print an integer SSS modulo nnn.

Hint

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

The second test is: S=1×1!+2×2!=5S = 1\times 1!+2 \times 2!= 5S=1×1!+2×2!=5 , and 555 modulo 333 is 222.

样例输入复制

2
2
3

样例输出复制

1
2

题目来源

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

代码高亮 Sublime Vim Emacs

环境配色亮色配色暗色配色 暗色配色

返回

  • 亮色配色
  • 暗色配色

代码缩进248 4

返回

  • 2
  • 4
  • 8

C 语言C++ 语言 (C++11)Java 语言 C++ 语言 (C++11)

返回

  • C 语言
  • C++ 语言 (C++11)
  • Java 语言
  • main.cpp

通用

逻辑

循环

数学

文本

列表

颜色

变量

函数

 

 

 

1

?

关闭终端 终端 - 计蒜客

只看题面

 

 

#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long
const int  maxn =3e3+5;
const int mod=1e9+7;ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
ll powmod(ll x,ll y){ll t;for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod;return t;}ll n;
/*
感觉就像是高考的数学填空题。。。。
简单的证明下吧(比赛时大力了一发,居然A了T_T。)
sigma i:(1~n-1) i*(i!)。
令其为p1,
令sigma i:(1~n-1) (i!) 为p2;
p1+p2=sigma i:(2~n) i! mod n;
p1+p2=sigma i:(1~n) i! mod n +n-1,
可以看到p1 mod n =n-1。
*/int main()
{int t;scanf("%d",&t);while(t--){scanf("%lld",&n);///T_Tprintf("%lld\n",n-1);///T_T}return 0;
}

 

  相关解决方案