class
Championship {
public
:
int
getGcd(
int
a,
int
b) {
// 辗转相除法计算最大公约数
if
(a ==
0
)
return
b;
if
(b ==
0
)
return
a;
int
temp;
while
(b !=
0
) {
temp = a % b;
a = b;
b = temp;
}
return
a;
}
vector<
int
> calc(
int
k) {
int
n1 =
1
,n2 =
1
;
vector<
int
> res;
for
(
int
i =
2
*k-
1
;i >=
3
;i = i-
2
)
n1 *= i;
n2 = (k+
1
)*k/
2
;
for
(
int
j = k-
1
;j >=
1
;j--)
n2 *= j;
n2 = n1-n2;
int
gcd = getGcd(n1,n2);
res.push_back(n2/gcd);
res.push_back(n1/gcd);
return
res;
}
};
class
Ants {
public
:
vector<
int
> collision(
int
n) {
int
C,D;
C =
1
; D =
1
<< (n -
1
);
vector<
int
> res;
res.push_back(D - C); res.push_back(D);
return
res;
}
};
class
Ants {
public
:
int
quickPower(
int
a,
int
n) {// 快速幂算法
long
res =
1
;
long
base = a;
while
(n) {
if
(n&
1
==
1
)
res *= base;
base *= base;
n >>=
1
;
}
return
(
int
)res;
}
int
getGcd(
int
a,
int
b) {// 求gcd的移位算法
if
(a ==
0
)
return
b;
if
(b ==
0
)
return
a;
if
(!(a&
1
) && !(b&
1
))
return
getGcd(a>>
1
,b>>
1
) <<
1
;
else
if
(!(a&
1
))
return
getGcd(a>>
1
,b);
else
if
(!(b&
1
))
return
getGcd(a,b>>
1
);
else
return
getGcd(abs(a-b),min(a,b));
}
vector<
int
> collision(
int
n) {
vector<
int
> res(
2
,
0
);
int
n1 = quickPower(
2
,n);
int
n2 = n1-
2
;
int
gcd = getGcd(n1,n2);
res[
0
] = n2/gcd;
res[
1
] = n1/gcd;
return
res;
}
};
class
Random7 {
public
:
int
rand5() {
return
Random5::randomNumber();
}
int
randomNumber() {
int
temp;
do
{
temp =
5
*(rand5()-
1
)+(rand5()-
1
);
}
while
(temp >
20
);
return
(temp%
7
)+
1
;
}
};
class
RandomSeg {
public
:
double
f() {
return
rand() *
1.0
/ RAND_MAX;
}
double
random(
int
k,
double
x) {
double
res=-
1
;
for
(
int
i=
0
;i
res=max(res,f());
}
return
res;
}
};
推荐博文:
经典排序算法的C++实现