根据唯一分解定理 将一个数质因子分解
A = p1^k1 * p2^k2 * p3^k3..............*pn^kn;
因为p1, p2,....pn均为素数, pn^kn因子个数为( k1 + 1)个 , A的所有因子个数为
( k1+1)(k2+1)......*( kn+1)个 就是从每一个部分中选取一个数
A的因子和为( 1 + p1 + p1^2 +.....+ p1^k1 )* ( 1 + p2 + p2^2 +......+ p2^k2).........*( 1+pn + pn^2 +.....+pn^kn )
则 A^B = p1^ Bk1 * p2 ^ Bk2 * p3 ^ Bk3..............*pn ^ Bkn;
例如12 = 2^2 * 3 因子为 1 2 3 4 6 12
因子个数为 ( 2+1)( 1+1) = 6;
因子和为 ( 2^0 + 2^ 1 + 2^2 )* ( 3^0 + 3^ 1 ) = 28;
因子个数与因子同样求法 一般需要取模
下面我们就看看怎么求取因子和 很显然这是几个等比数列的乘积
今天我们学习如何有效地求表达式的值。对于这个问题,用二分解决比较好。
(1)当时,
(2)当时,那么有
(3)当时,那么有
通过递推公式 很容易写出代码
核心代码
ll sum( ll a, ll b )
{
if( b == 1 )
{
return a;
}
if( b%2 == 0 )
{
return sum( a, b/2 )*( 1+ quick( a, b/2 ))%k;
}
else
if( b%2 == 1 )
{
return (sum( a, ( b-1)/2)*( 1+quick( a, ( b-1)/2+1))%k+quick( a, ( b-1)/2+1))%k;
}
}
例题跟踪
As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a little special somehow. You are looking forward to it, too, aren't you? Unfortunately there still are months to go. Take it easy. Luckily you meet me. I have a problem for you to solve. Enjoy your time.
Now given a positive integer N, get the sum S of all positive integer divisors of 2008 N. Oh no, the result may be much larger than you can think. But it is OK to determine the rest of the division of S by K. The result is kept as M.
Pay attention! M is not the answer we want. If you can get 2008 M, that will be wonderful. If it is larger than K, leave it modulo K to the output. See the example for N = 1,K = 10000: The positive integer divisors of 20081 are 1、2、4、8、251、502、1004、2008,S = 3780, M = 3780, 2008 M % K = 5776.
Input
The input consists of several test cases. Each test case contains a line with two integers N and K (1 ≤ N ≤ 10000000, 500 ≤ K ≤ 10000). N = K = 0 ends the input file and should not be processed.
Output
For each test case, in a separate line, please output the result.
Sample Input
1 10000
0 0
Sample Output
5776
题目有点绕 , 输入 n, k, m值 为2008^n的所有因子mod k, 然后 再求 2008 ^ m%k
显然就是 先等比求出m 然后快速幂求结果
其实还需要质因子分解 但本题 2008明显为 2^3 * 251 我直接用了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll n ,m ,k ;
ll quick( ll a, ll b )
{
ll c = k;
ll ans = 1;
a = a%c;
while( b > 0 )
{
if( b&1 )
{
ans = ( ans*a)%c;
}
a = ( a*a )%c;
b = b/2;
}
return ans;
}
ll sum( ll a, ll b )
{
if( b == 1 )
{
return a;
}
if( b%2 == 0 )
{
return sum( a, b/2 )*( 1+ quick( a, b/2 ))%k;
}
else
if( b%2 == 1 )
{
return (sum( a, ( b-1)/2)*( 1+quick( a, ( b-1)/2+1))%k+quick( a, ( b-1)/2+1))%k;
}
}
int main()
{
while(scanf("%lld%lld", &n, &k) != EOF)
{
if( n == 0 && k == 0 )
return 0;
m = (sum( 2, 3*n)+1)%k*( sum( 251, n )+1)%k;
//cout<<m<<endl;
ll res = quick( 2008, m);
printf("%I64d\n", res);
}
return 0;
}