forked from Ouditchya/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZSUM.cpp
61 lines (41 loc) · 1.09 KB
/
ZSUM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// AC, ALGO : Math, Exponentiation by Squaring.
// Just solve the expression Z(n) + Z(n-1) - 2 * Z(n-2) using S(n) & P(n), most of the terms will cancel.
/* Some Helpful Links :
http://en.wikipedia.org/wiki/Exponentiation_by_squaring
http://www.programminglogic.com/fast-exponentiation-algorithms/
*/
// For any clarifications, contact me at : [email protected]
#include<cstdio>
#define MOD 10000007
using namespace std ;
typedef long long int int_64 ;
int_64 simple_exponentiation( int_64 a , int_64 b )
{
int_64 x = 1 , y = a ;
while( b )
{
if( b & 1 )
x = ( x * y ) % MOD ;
y = ( y * y ) % MOD ;
b >>= 1 ;
}
return x % MOD ;
}
int main()
{
int_64 n , k , a , b , c , d , ans ;
scanf("%lld%lld",&n,&k) ;
while( n != 0 && k != 0 )
{
a = simple_exponentiation( n , k ) ;
b = simple_exponentiation( n - 1 , k ) ;
b = ( b * 2 ) % MOD ;
c = simple_exponentiation( n , n ) ;
d = simple_exponentiation( n - 1, n - 1 ) ;
d = ( d * 2 ) % MOD ;
ans = ( a + b + c + d ) % MOD ;
printf("%lld\n",ans) ;
scanf("%lld %lld",&n,&k) ;
}
return 0 ;
}