-
Notifications
You must be signed in to change notification settings - Fork 67
/
ADADIG.cpp
84 lines (73 loc) · 1.93 KB
/
ADADIG.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// main.cpp
// practice
//
// Created by Mahmud on 9/16/17.
// Copyright © 2017 Mahmud. All rights reserved.
//
/*
A simple recursive solution
The main point is that we can not use more than log2(N) digits which are not equal to 1.
So we just need to consider the possible combinations of digits between 2 and 9.
And the rest will be ones.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
const int MAX = 300005;
const int modulo = 1000000007;
int T, N;
int result;
int fac[MAX];
int binaryPower (int a, int b, int modulo) {
if (b == 0) {
return 1 % modulo;
}
if (b & 1) {
return 1LL * binaryPower(a, b - 1, modulo) * a % modulo;
}
int half = binaryPower(a, b >> 1, modulo);
return 1LL * half * half % modulo;
}
int inverse (int a, int modulo) {
return binaryPower(a, modulo - 2, modulo);
}
void go (int digit, int product, int sum, int takenDigits, int denominator) {
if (sum > N) {
return;
}
if (product == 1) {
int ones = N - sum;
int numerator = fac[takenDigits + ones];
denominator = 1LL * denominator * fac[ones] % modulo;
result = (result + 1LL * numerator * inverse(denominator, modulo) % modulo) % modulo;
return;
}
if (digit > 9) {
return;
}
go(digit + 1, product, sum, takenDigits, denominator);
int count = 0;
while (product % digit == 0) {
count ++;
product /= digit;
go(digit + 1, product, sum + digit * count, takenDigits + count, 1LL * denominator * fac[count] % modulo);
}
}
int main(int argc, const char * argv[]) {
fac[0] = 1;
for (int i = 1; i < MAX; i ++) {
fac[i] = 1LL * fac[i - 1] * i % modulo;
}
scanf("%d", &T);
while (T --) {
scanf("%d", &N);
result = 0;
go(2, N, 0, 0, 1);
printf("%d\n", result);
}
return 0;
}