1. 程式人生 > >[絕對能看懂!]Deciphering Password(積性函式+線性篩+素數分解)

[絕對能看懂!]Deciphering Password(積性函式+線性篩+素數分解)

描述

Xiaoming has just come up with a new way for encryption, by calculating the key from a publicly viewable number in the following way:

Let the public key N = A^B, where 1 <= A, B <= 1000000, and a0, a1, a2, …, ak-1 be the factors of N, then the private key M is calculated by summing the cube of number of factors of all ais. For example, if A is 2 and B is 3, then N = A^B = 8, a0 = 1, a1 = 2, a2 = 4, a3 = 8, so the value of M is 1 + 8 + 27 + 64 = 100.

However, contrary to what Xiaoming believes, this encryption scheme is extremely vulnerable. Can you write a program to prove it?

輸入

There are multiple test cases in the input file.

Each test case starts with two integers A, and B. (1 <= A, B <= 1000000). Input ends with End-of-File.

輸出

For each test case, output the value of M (mod 10007) in the format as indicated in the sample output.

提示

2008 Asia Hangzhou Regional Contest Online.

分析

給定A,B,求 N = A B N=A^B 的各個因子的因子個數的立方和。比較拗口,用公式敘述。設N的因子為 a

1 , a 2 , . . . , a k a_1,a_2,...,a_k ,對於任意 a i ( 1 i k ) a_i (1\leq i\leq k) ,求 a i a_i 的因子個數 ,然後求得最終結果 M = i = 0 k n M=\sum_{i=0}^{k}n ,輸出結果取餘10007。
定義1:函式 d ( a ) d(a) a a 的因子個數。例如,4的因子個數為3個,分別是1,2,4,則 d ( 4 ) = 3 d(4)=3
定義2:函式 D ( N ) D(N) N N 的各個因子的立方和,即 D ( N ) = m N m 3 D(N)=\sum_{m|N}m^3
引理1 d ( a ) d(a) D ( N ) D(N) 是積性函式(multiplicative function)。事實上兩者是除數函式(divisor function)的兩種特殊情況,而除數函式是積性函式。積性函式 f ( x ) f(x) 具有如下性質:
(1) f ( 1 ) = 1 f(1)=1
(2) f ( a b ) = f ( a ) f ( b ) f(a\cdot b)=f(a)\cdot f(b) ,當且僅當a,b互質
滿足上面兩條性質,那麼我們說 f ( x ) f(x) 是積性函式。
引理2:積性函式的除數函式還是積性函式。
證明(可略過):
{
假設 f ( x ) f(x) 是積性函式,令 D ( f ) D(f) f f 的除數函式。假設 ( m , n ) = 1 (m,n)=1 (即m,n互質),則有:
[ D ( f ) ] ( m ) = a m f ( a ) a n d [ D ( f ) ] ( n ) = b n f ( b ) [D(f)](m)=\sum_{a|m}f(a) \quad and \quad [D(f)](n)=\sum_{b|n}f(b)
即:
[ D ( f ) ] ( m ) [ D ( f ) ] ( n ) = ( a m f ( a ) ) ( b n f ( b ) ) = a m b n f ( a ) f ( b ) [D(f)](m)\cdot [D(f)](n)=\left( \sum_{a|m}f(a)\right)\left( \sum_{b|n}f(b)\right) =\sum_{a|m} \sum_{b|n} f(a)f(b)
由於 ( m , n ) = 1 (m,n)=1 a m a|m b n b|n ,故 ( a , b ) = 1 (a,b)=1 (證明略,高代基礎)。因此由 f ( x ) f(x) 為積性函式可得:
[ D ( f ) ] ( m ) [ D ( f ) ] ( n ) = a m b n f ( a b ) [D(f)](m)\cdot [D(f)](n)=\sum_{a|m} \sum_{b|n} f(ab)
對於 m n mn 的任意一個因子,我們都可以將其寫作 d = a b d=ab 其中 a m a|m b n b|n 。故:
[ D ( f ) ] ( m ) [ D ( f ) ] ( n ) = [ D ( f ) ] ( m n ) [D(f)](m)\cdot [D(f)](n)=[D(f)](m\cdot n)
得證 D ( f ) D(f) 為積性函式。
參考:
http://sites.millersville.edu/bikenaga/number-theory/divisor-functions/divisor-functions.html
}

正式解題:

題目要我們求的其實就是 [ D ( d ) ] ( N ) = a N d ( a ) [D(d)](N)=\sum_{a|N}d(a)