ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[HDU5391]Zball in Tina Town

2021-06-25 20:35:25  阅读:202  来源: 互联网

标签:Zball Town ch HDU5391 times int include day define


Description
Tina Town is a friendly place. People there care about each other.

Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day, it becomes 1 time as large as its original size. On the second day,it will become 2 times as large as the size on the first day. On the n-th day,it will become nn times as large as the size on the (n-1)-th day. Tina want to know its size on the (n-1)-th day modulo n.

Input
The first line of input contains an integer \(T\), representing the number of cases.
The following \(T\) lines, each line contains an integer \(n\), according to the description.
\((T\leqslant 10^5,2\leqslant n\leqslant 10^9)\)

Output
For each test case, output an integer representing the answer.

Sample Input

2
3
10

Sample Output

2
0

题目大意:求\((n-1)!\%n\)

根据打表+找规律+大胆猜想可得,当\(n\)为质数时,答案为\(n-1\),否则答案为\(0\)(当\(n=4\)时,答案为2,是特殊情况)

下面我们给出其证明:

  • \(n=4\),直接模拟即可

  • \(n\)为合数,但不为完全平方数,则此时令\(n=a\times b,a\neq b\),故必然有\(a,b<n-1\),则\((n-1)!\%n=0\)

  • \(n\)为完全平方数(4除外),则令\(n=a^2\),因为\(a>2\),故\(2a<n-1\),则\((n-1)!\%n=0\)

  • \(n\)为质数,则可套用威尔逊定理:当且仅当\(p\)为质数时,\((p-1)!\equiv -1\mod p\)

    下面给出威尔逊定理的证明:

    易得\(1\times 1\equiv1\mod p,(-1)\times(-1)\equiv1\mod p\),且在小于\(p\)的所有数中,仅有这两组逆元与本身相等(-1即为p-1)

    除此之外,\(2...p-2\)中所有数都有一个对应的逆元,且逆元与本身不相同,而且这个逆元关系是一一对应的

    \(a\times a^{-1}\equiv1\),则有\(a^{-1}\times a\equiv1\),故\((a^{-1})^{-1}=a\),说明逆元的关系是可逆的,即逆元是成对出现的

    如果\(p=2\),结论显然成立;如果\(p>2\),那么\(p\)必然为奇数,故\(2...p-2\)的个数是偶数,即所有数都两两配对互为逆元,那么它们的乘积\(\%p\)后都为1,最后再乘上1和-1,故最终的结果为-1,得证

/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define ll_inf 1e18
#define MK make_pair
#define sqr(x) ((x)*(x))
#define pii pair<int,int>
#define int_inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline T frd(T x){
	int f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
template<typename T>inline T read(T x){
	int f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
bool Check(int x){
	int n=sqrt(x);
	for (int i=2;i<=n;i++)
		if (x%i==0)
			return 0;
	return 1;
}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read(0);
	while (T--){
		int n=read(0);
		if (n==4){
			printf("%d\n",2);
			continue;
		}
		printf("%d\n",Check(n)?n-1:0);
	}
	return 0;
}

标签:Zball,Town,ch,HDU5391,times,int,include,day,define
来源: https://www.cnblogs.com/Wolfycz/p/14932418.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有