HDU 5391 Zball in Tina Town【威尔逊定理】 Zball in Tina Town
<题目链接>
Problem 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 n times as large as the size on the (n-1)-th day. Tina want to know its size on the (n-1)-th day mod n.
Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day, it becomes n times as large as the size on the (n-1)-th day. Tina want to know its size on the (n-1)-th day mod n.
Input
The first line of input contains an integer 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-1)! %n=n-1 ,但是如果n不是素数,而是合数,应该怎么办呢?我们可以简单的试几个数,如果n为合数,我们发现
此题一看到 (n-1)! 就要立刻想到威尔逊定理,当n为素数时,毫无疑问 (n-1)! %n=n-1 ,但是如果n不是素数,而是合数,应该怎么办呢?我们可以简单的试几个数,如果n为合数,我们发现
(n-1)!中总能够找出一些因子,使它们想乘为n,但是要注意4是个例外,由于4比较靠前,所以(4-1)!不一定能够凑出4,除了这种情况,其他的合数毫无疑问答案都为0。
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; int juge(int x) { for(int i=2;i*i<=x;i++) { if(x%i==0)return false; } return true; } int main() { int t;cin>>t; while(t--) { int n; scanf("%d",&n); if(n==4) { printf("2 "); } else { if(juge(n)) { printf("%d ",n-1); } else printf("0 "); } } return 0; }
2018-07-31