1. 程式人生 > >Bzoj2219 數論之神

Bzoj2219 數論之神

優化 rdquo open ace txt earch color 質因數 範圍

Time Limit: 3 Sec Memory Limit: 259 MB
Submit: 954 Solved: 268

Description

在ACM_DIY群中,有一位叫做“傻崽”的同學由於在數論方面造詣很高,被稱為數輪之神!對於任何數論問題,他都能瞬間秒殺!一天他在群裏面問了一個神題: 對於給定的3個非負整數 A,B,K 求出滿足 (1) X^A = B(mod 2*K + 1) (2) X 在範圍[0, 2K] 內的X的個數!自然數論之神是可以瞬間秒殺此題的,那麽你呢?

Input

第一行有一個正整數T,表示接下來的數據的組數( T <= 1000) 之後對於每組數據,給出了3個整數A,B,K (1 <= A, B <= 10^9, 1 <= K <= 5 * 10^8)

Output

輸出一行,表示答案

Sample Input

3
213 46290770 80175784
3 46290770 80175784
3333 46290770 80175784

Sample Output

27
27
297

HINT

新加數組一組--2015.02.27

Source

數論 鳴謝 AekdyCoin

數學問題 原根 階 指標 中國剩余定理 腦洞題

吼題

學姐的講解很棒棒 http://blog.csdn.net/regina8023/article/details/44863519

模數$P=2*K+1$很大,在這個範圍下沒什麽方法可以有效計算,所以需要優化數據範圍。

將P分解質因數,$p_{1}^{a1}+p_{2}^{a2}+p_{3}^{a3}+...$

分別在每個模$ p_{i}^{ai} $ 的意義下計算出答案,將這些答案累乘起來就是最終的答案。(在每個模意義下選出一個解,則構成了一組同余方程,根據中國剩余定理,每組同余方程對應一個唯一解,所以可以用乘法原理統計總答案數)

假設當前我們在處理一個$p^{a}$

現在需要求$x^a \equiv b (\mod p^{a})$的解個數

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4
#include<cmath> 5 #include<cstring> 6 #define LL long long 7 using namespace std; 8 const int mod=1e9+7; 9 const int mxn=1000010; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<0 || ch>9){if(ch==-)f=-1;ch=getchar();} 13 while(ch>=0 && ch<=9){x=x*10+ch-0;ch=getchar();} 14 return x*f; 15 } 16 int n; 17 int a[mxn]; 18 int f[mxn]; 19 int main(){ 20 // freopen("in.txt","r",stdin); 21 int i,j,cnt=0; 22 n=read(); 23 for(i=1;i<=n;i++){a[i]=read();if(a[i]==1)cnt++;} 24 f[0]=1;f[1]=1;f[2]=2; 25 for(i=3;i<=cnt;i++)f[i]=((LL)f[i-1]+(LL)f[i-2]*(i-1)%mod)%mod; 26 for(i=n;i>cnt;i--)f[cnt]=(LL)f[cnt]*i%mod; 27 printf("%d\n",f[cnt]); 28 return 0; 29 }

Bzoj2219 數論之神