-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuttingRope.cpp
More file actions
122 lines (109 loc) · 1.94 KB
/
cuttingRope.cpp
File metadata and controls
122 lines (109 loc) · 1.94 KB
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
116
117
118
119
120
121
122
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
// int cuttingRope(int n)
// {
// if (n < 2)
// {
// return 0;
// }
// if (n == 2)
// {
// return 1;
// }
// if (n == 3)
// {
// return 2;
// }
// int *res = new int[n + 1];
// int max = 0;
// res[1] = 1;
// res[2] = 2;
// res[3] = 3;
// for (int i = 4; i <= n; i++)
// { max = 0;
// for (int j = 1; j <= i/2; j++)
// {
// int tmp = res[j] * res[i - j];
// if (max < tmp){
// max = tmp;
// }
// }
// res[i] = max;
// }
// max = res[n];
// return max;
// }
// int pows(int n, int m)
// {
// int res = 1;
// if (m == 3)
// {
// for (int i = 0; i < n; i++)
// {
// res = (res * 3) % 1000000007;
// }
// return res;
// }
// else
// {
// for (int i = 0; i < n; i++)
// {
// res = (res * 2) % 1000000007;
// }
// return res;
// }
// }
// int cuttingRope(int n)
// {
// if (n < 2)
// {
// return 1;
// }
// if (n == 2)
// {
// return 1;
// }
// if (n == 3)
// {
// return 2;
// }
// int timeThree = n / 3;
// if (n - timeThree * 3 == 1)
// {
// timeThree--;
// }
// int timeTwo = (n - timeThree * 3) / 2;
// int max = pows(timeThree, 3) * pows(timeTwo, 2);
// return max % 1000000007;
// }
int cuttingRope(int n)
{
if (n < 2)
{
return 1;
}
if (n == 2)
{
return 1;
}
if (n == 3)
{
return 2;
}
long long res = 1;
while (n > 4)
{
res *= 3;
res %= 1000000007;
n -= 3;
}
return res * n % 1000000007;
}
int main()
{
int n = cuttingRope(127);
printf("%d",n);
return 0;
}