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

Zball in Tina Town

时间:2015-08-16 21:18:18      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

Zball in Tina Town

 
 Accepts: 356
 
 Submissions: 2463
 Time Limit: 3000/1500 MS (Java/Others)
 
 Memory Limit: 262144/262144 K (Java/Others)
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 11 time as large as its original size. On the second day,it will become 22times 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 TT, representing the number of cases.

The following TT lines, each line contains an integer nn, according to the description. T \leq {10}^{5},2 \leq n \leq {10}^{9}T10?5??,2n10?9??

Output

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

Sample Input
2
3
10
Sample Output
2
0

 题意:一个球的原始体积是1,到第n天增大到它的n-1的n倍,体积就是阶乘的意思,结果让我们求n-1天的体积对n的余数,10的9次方,那么大。是,有规律

如果n是素数那么余数就是n-1,如果不是素数对n的余数肯定是0,当然4除外,是2.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

#define N 100000
#define INF 0xfffffff

int num[N], k;
int a[N] = {1, 1};

void prim()
{
    k = 0;
    //memset(num, 0, sizeof(num));

    for(int i = 2; i < N; i++)
    {
        if(!a[i])
            num[k++] = i;
        for(int j = i+i; j < N; j += i)
            a[j] = 1;
    }
}

int isprime(int n)
{
    if(n == 0 || n == 1)
        return 0;

    for(int i = 0; (long long)num[i]*num[i] <= n; i++)
        if(n % num[i] == 0)
            return false;
    return true;
}

int main()
{
    int t, n;

    scanf("%d", &t);
    prim();

    while(t--)
    {
        scanf("%d", &n);
        if(n <= 4)
            printf("2\n");
        else if(isprime(n))
            printf("%d\n", n-1);
        else
            puts("0");
    }
    return 0;
}

 

Zball in Tina Town

标签:

原文地址:http://www.cnblogs.com/Tinamei/p/4734963.html

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