码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5391 Zball in Tina Town【威尔逊定理】

时间:2018-07-31 23:35:44      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:integer   res   pre   answer   除了   ant   printf   威尔逊   答案   

<题目链接>

                              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 1 time as large as its original size. On the second day,it will become 2times as large as the size on the first day. On the n-th day,it will become 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 T, representing the number of cases.
The following T lines, each line contains an integer n, according to the description.
T105,2n109
 
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,但是要注意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\n");
        } 
        else
        {
            if(juge(n))
            {
                printf("%d\n",n-1);
            }
            else
            printf("0\n");
        }        
    }
    return 0;
}
 
 
2018-07-31

HDU 5391 Zball in Tina Town【威尔逊定理】

标签:integer   res   pre   answer   除了   ant   printf   威尔逊   答案   

原文地址:https://www.cnblogs.com/00isok/p/9398494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!