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 become2 times
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 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≤105,2≤n≤109
 

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

Sample Input

2
3
10
 

Sample Output

2
0

这题求的是(n-1)!%n,根据打表发现,当n是4的时候输出2,其他如果是素数的话就输出n-1,否则输出0.用了线性素数筛法,时间少了很多。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 400000
int prime[maxn+10],vis[maxn+10],tot;
void init()
{
int i,j;
tot=0;
for(i=2;i<=maxn;i++){
if(!vis[i]){
tot++;prime[tot]=i;
}
for(j=1;j<=tot &&prime[j]*i<=maxn;j++){
vis[prime[j]*i]=1;
if(i%prime[j]==0)break;
}
}
} int main()
{
int n,m,i,j,num,T,flag;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
if(n==4){
printf("2\n");continue;
}
if(n<=maxn){
if(!vis[n]){
printf("%d\n",n-1);
}
else printf("0\n");
continue;
}
flag=1;
for(i=1;i<=tot && prime[i]*prime[i]<=n;i++){
if(n%prime[i]==0){
flag=0;break;
}
}
if(flag)printf("%d\n",n-1);
else printf("0\n");
}
return 0;
}

hdu5391 Zball in Tina Town的更多相关文章

  1. hdu5391 Zball in Tina Town(威尔逊定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Zball in Tina Town Time Limit: 3000/1500 ...

  2. HDU-5391 Zball in Tina Town

    (n-1)!/n 就是如果n为素数,就等于n-1else为0. 求素数表: Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others) Memo ...

  3. C#版 - HDUoj 5391 - Zball in Tina Town(素数) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. HDUoj 5 ...

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

    <题目链接> Zball in Tina Town Problem Description Tina Town is a friendly place. People there care ...

  5. hdu 5391 Zball in Tina Town 威尔逊定理 数学

    Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  6. 判素数+找规律 BestCoder Round #51 (div.2) 1001 Zball in Tina Town

    题目传送门 /* 题意: 求(n-1)! mod n 数论:没啥意思,打个表能发现规律,但坑点是4时要特判! */ /***************************************** ...

  7. BC - Zball in Tina Town (质数 + 找规律)

    Zball in Tina Town  Accepts: 541  Submissions: 2463  Time Limit: 3000/1500 MS (Java/Others)  Memory ...

  8. Zball in Tina Town

    Zball in Tina Town  Accepts: 356  Submissions: 2463  Time Limit: 3000/1500 MS (Java/Others)  Memory ...

  9. (hdu)5391 Zball in Tina Town

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 Problem Description Tina Town is a friendl ...

随机推荐

  1. InheritableThreadlocal使用问题排查

    背景 在做一个微服务系统的时候,我们的参数一般都是接在通过方法定义来进行传递的,类似这样 public void xxx(Param p, ...){ // do something } 然后这时有个 ...

  2. mysql的逻辑备份和恢复

    备份指定的数据库或此数据库中的某些表 mysqldump [options] db_name [tables] >backup.sql 备份指定的一个或多个数据库 mysqldump --dat ...

  3. Nginx(六):配置解析之location解析

    nginx成为非常流行的代理服务软件,最根本的原因也许是在于其强悍性能.但还有一些必要的条件,比如功能的完整,配置的易用,能够解决各种各样的实际需求问题,这些是一个好的软件的必备特性. 那么,今天我们 ...

  4. 消息队列之activeMQ

    1.activeMQ的主要功能 实现高可用.高伸缩.高性能.易用和安全的企业级面向消息服务的系统 异步消息的消费和处理 控制消息的消费顺序 可以和Spring/springBoot整合简化编码 配置集 ...

  5. pyinstaller打包shotgun有关的程序

    By 鬼猫猫 http://www.cnblogs.com/muyr/ 背景 使用pyinstaller打包跟shotgun有关的程序后,在自己电脑上运行都OK,但是编译好的exe在其他人的电脑上运行 ...

  6. uni-app通过canvas实现手写签名

    分享一个uni-app实现手写签名的方法 具体代码如下: <template> <view > <view class="title">请在下面 ...

  7. ASP Net Core – CORS 预检请求

    CORS(跨源资源共享)是一种机制,它允许同一个来源运行的Web应用程序从在另一个来源运行的服务器访问资源.同源策略是一种非常严格的措施,因为它只允许与服务器起源于同一源的应用程序访问其资源.很多时候 ...

  8. Android事件分发机制四:学了事件分发有什么用?

    " 学了事件分发,影响我CV大法吗?" " 影响我陪女朋友的时间" " ..... " 前言 Android事件分发机制已经来到第四篇了,在 ...

  9. STL_常用的算法

    STL_常用的算法 一.常用的查找算法 adjacent_find() adjacent_find(iterator beg, iterator end, _callback); 在iterator对 ...

  10. C#高级编程第11版 - 第五章 索引

    [1]5.1 泛型概述 1.通过泛型,你可以创建独立于特定类型(contained types)以外的方法和类,而不用为不同类型编写多份同样功能的代码,你只需要创建一个方法或者类. 2.泛型类使用泛型 ...