-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsum.c
83 lines (67 loc) · 1.08 KB
/
zsum.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <string.h>
typedef unsigned long long ull;
#define PRIME 10000007
#define LIMIT 28
ull powers[LIMIT];
int positions[LIMIT];
int binary(int n)
{
int cnt=0;
memset(positions,0,LIMIT*sizeof(int));
while(n)
{
if(n&1)
positions[cnt]=1;
n>>=1;
cnt+=1;
}
return cnt;
}
int compute(int n, int limit)
{
int i=0;
ull prod=0;
memset(powers,0,LIMIT*sizeof(ull));
powers[0]=n%PRIME;
for(i=1;i<limit;++i)
{
prod=powers[i-1]*powers[i-1];
powers[i]=prod%PRIME;
}
}
int power(int n, int k)
{
int cnt=binary(k);
compute(n,cnt);
ull res=1;
int i=0;
for(i=0;i<LIMIT;++i)
if(positions[i])
res=(res*powers[i])%PRIME;
return (int)res%PRIME;
}
int solution(int n, int k)
{
ull res=0;
int t1=power(n-1,k);
t1<<=1;
int t2=power(n-1,n-1);
t2<<=1;
res=power(n,k);
res=(res+power(n,n))%PRIME;
res=(res+t1)%PRIME;
res=(res+t2)%PRIME;
return (int)(res%PRIME);
}
int main()
{
int n,k;
while(1)
{
scanf("%d %d",&n,&k);
if(!n && !k) break;
printf("%d\n",solution(n,k));
}
return 0;
}