在线提交: http://acm.hdu.edu.cn/showproblem.php?pid=5391 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 2790 Accepted Submission(s): 1309
题目大意: Tina Town 是一个善良友好的地方, 这里的每一个人都互相关心。 Tina有一个球,它的名字叫zball。zball很神奇,它每天会变大一些。在第一天,它和原始大小一样。 在第二天,它的大小将成为第一天的2倍。 在第n天,它的大小将为第(n-1)天大小的n倍。Tina想知道,zball在第n-1天时的大小对n取模是多少呢?
思路: 陶哲轩在他的书Solving mathematical problems 中提到威尔逊定理(n−1)!+1 (mod n)≡0⇔n(n−1)!+1 (mod n)≡0⇔n(n-1)! + 1\ (mod\ n) \equiv 0 \Leftrightarrow n is prime.
首先,来回忆一下阶乘的定义: m!=∏mk=1k=1×2×3×⋯×m.m!=∏k=1mk=1×2×3×⋯×m.m! = \prod_{k=1}^m k = 1 \times 2 \times 3 \times \dots \times m.
可得出结论: 存在a, b ∈∈\in [1, m] 使得a⋅ba⋅ba\cdot b能整除m!
假定 m=n−1m=n−1m = n-1,
原问题可分类如下:
已AC代码:
using System;
namespace hdoj_zball
{
public class Solution
{
public int Mod(int n)
{
if (IsPrime(n))
return n - 1;
if (n == 4)
return 2;
return 0;
}
public bool IsPrime(int m)
{
if (m < 2)
return false;
for (int i = 2; i * i <= m; i++)
{
if (m % i == 0)
return false;
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
while (n-- > 0)
{
string[] s = Console.ReadLine().Split();
int num = int.Parse(s[0]);
Solution sol = new Solution();
Console.WriteLine(sol.Mod(num));
}
}
}
}
Rank (C#):
Exe.Time | Exe.Memory |
---|---|
1185ms | 26908K |
Reference: elementary number theory - If n≠4n≠4n\ne 4 is composite, then nnn divides (n−1)!(n−1)!(n-1)!. - Mathematics Stack Exchange https://math.stackexchange.com/questions/164852/if-n-ne-4-is-composite-then-n-divides-n-1