HDU 5391 Zball in Tina Town
Zball in Tina Town
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1371 Accepted Submission(s): 708
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 2 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.
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 2 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
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
找规律 然后大整数判断素数这里有个大整数判断素数模板
/* *********************************************** Author :guanjun Created Time :2015/10/13 20:30:38 File Name :1.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #include <ctime> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b){ return a>b; } ll gcd(ll a,ll b){ return a==0?b:gcd(b%a,a); } ll mod_mul(ll a,ll b,ll n){ ll res=0; while(b){ if(b&1)res=(res+a)%n; a=(a+a)%n; b>>=1; } return res; } ll mod_exp(ll a,ll b,ll n){ ll res=1; while(b){ if(b&1)res=mod_mul(res,a,n); a=mod_mul(a,a,n); b>>=1; } return res; } bool miller_rabin(ll n){ if(n==2||n==3||n==5||n==7||n==11)return true; if(n==1||!(n%2)||!(n%3)||!(n%5)||!(n%7)||!(n%11))return false; ll x,pre,u; int i,j,k=0; u=n-1; while(!(u&1)){ k++;u>>=1; } srand((ll)time(0)); for(i=0;i<5;i++){//5次足够AC了 x=rand()%(n-2)+2; if((x%n)==0)continue; x=mod_exp(x,u,n); pre=x; for(j=0;j<k;j++){ x=mod_mul(x,x,n); if(x==1&&pre!=1&&pre!=n-1)return false; pre=x; } if(x!=1)return false; } return true; } ll w[21]={0,1, 2, 6, 24, 120, 720, 5040,40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000,355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000}; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int t; cin>>t; ll n; while(t--){ scanf("%I64d",&n); if(n<=21)printf("%I64d\n",w[n-1]%n); else { if(!miller_rabin(n))printf("%d\n",0); else printf("%I64d\n",n-1); } } return 0; }
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号: