hdu 5628 (Dirichlet积与快速幂的应用)

突然有点想打一场BestCoder,然后就看了看BC发现有道应用Dirichlet积的题。于是就去试了试。当然老套路还是很好过的,后来我想写的优美一点写成类的形式,不过一直出现Crash,于是跟周学长讨论了一下,外加一点看书,终于也是搞定了,就此存一下模版吧 0.0

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//#pragma comment(linker,"/STACK:10240000,10240000")
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define clr(a,b) memset(a,b,sizeof(a))
#define MP make_pair
#define PB push_back
#define lrt rt<<1
#define rrt rt<<1|1
#define lson l,m,lrt
#define rson m+1,r,rrt
/*------ Welcome to visit blog of dna049: http://dna049.com ------*/
class NumFun{
const static int M = 1000000007;
public:
int n;
int *a;
NumFun(int _n = 0){
n = _n;
a = new int[n+1];
}
NumFun(const NumFun &A){
n = A.n;
a = new int[n+1];
for(int i=1;i<=n;++i){
a[i]=A.a[i];
}
}
NumFun &operator=(const NumFun &A){
if(this != &A) {
delete [] a;
n = A.n;
a = new int[n+1];
for(int i=1;i<=n;++i){
a[i]=A.a[i];
}
}
return *this;
}
~NumFun(){
delete [] a;
}
void init(int d=0){
for(int i=1;i<=n;++i){
a[i]=d;
}
}
void set(int *p){
for(int i=1;i<=n;++i){
a[i]=p[i];
}
}
NumFun operator*(const NumFun &A)const{
NumFun R(n);
R.init(0);
for(int i=1;i<=n;++i){
for(int j=1;i*j<=n;++j){
R.a[i*j] = (R.a[i*j] + LL(a[i])*A.a[j])%M;
}
}
return R;
}
void print(){
for(int i=1;i<n;++i){
printf("%d ",a[i]);
}
printf("%d\n",a[n]);
}
};
NumFun pow(NumFun A,int k){
NumFun R(A.n);
R.init();
R.a[1]=1;
while(k){
if(k&1) R=R*A;
k>>=1; A=A*A;
}
return R;
}
const int N = 100005;
int a[N];
int main(){
// freopen("/Users/c10110057/Desktop/AC/in","r",stdin);
// freopen("/Users/c10110057/Desktop/AC/out","w",stdout);
int T,n,k;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;++i){
scanf("%d",a+i);
}
NumFun R(n);
R.set(a);
NumFun ONE(n);
ONE.init(1);
R = R*pow(ONE,k);
R.print();
}
return 0;
}