1、fact4
这个问题在《编程之美》上有过一个类似的问题,大概是求阶乘 n! 后面有多少个0,最后可以转换为求[1,N]之间因子5有多少次。这个问题和fact4有点渊源,对于 n! 的尾数,看看规律就知道了:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5,040
8! = 40,320
9! = 362,880
10! = 3,628,800
11! = 39,916,800
12! = 479,001,600
13! = 6,227,020,800
14! = 87,178,291,200
15! = 1,307,674,368,000
16! = 20,922,789,888,000
其实都是有 (n-1)! 的非0尾数与 n 乘积的尾数,唯一的麻烦是遇到了5,就会使得尾数变0,这时候要往进位去找新产生的尾数。好了,所有的 Trick 都在这了。我们看到题目指出 n 不大于4220 < 5^6,所以,代码如下,注意那个pow:
-
#include <fstream>
-
using
namespace std
;
-
-
ifstream fin
(
"fact4.in"
)
;
-
ofstream fout
(
"fact4.out"
)
;
-
-
int N
;
-
-
int main
(
)
-
{
-
fin
>> N
;
-
-
int multiplier
=
1,
pow
=
100000, factor
;
-
for
(
int i
=
1
; i
<= N
;
++i
)
-
{
-
factor
= i
%
pow
;
-
multiplier
= multiplier
* factor
;
-
-
while
(multiplier
%
10
==
0
)
-
multiplier
/
=
10
;
-
-
while
(multiplier
/
pow
>
0
)
-
multiplier
%
=
pow
;
-
}
-
-
fout
<< multiplier
%
10
<< endl
;
-
-
return
0
;
-
}
2、spin
单纯朴素模拟,借助STL的bitset来存储轮子们的位置信息,因为数据量实在是小,所以没必要用线段树来做时空优化了,要是用线段树,肯定要快N倍吧。然后其实你不用bitset,直接开一个bool数组也是OK的。
-
#include <fstream>
-
#include <bitset>
-
using
namespace std
;
-
ifstream fin
(
"spin.in"
)
;
-
ofstream fout
(
"spin.out"
)
;
-
-
#define PI 360
-
int speed
[
6
]
;
-
bitset
<PI
> curr
[
6
], next
[
6
]
;
-
-
int main
(
)
-
{
-
int num, start, end
;
-
for
(
int i
=
1
; i
<=
5
; i
++
)
-
{
-
fin
>> speed
[i
]
>> num
;
-
for
(
int j
=
1
; j
<= num
; j
++
)
-
{
-
fin
>> start
>> end
;
-
end
+
= start
;
-
for
(
int k
= start
; k
<= end
; k
++
)
-
curr
[i
].
set
(k
%PI
)
;
-
}
-
}
-
-
int angle
=
0
;
-
bool flag
;
-
while
(angle
< PI
)
-
{
-
for
(
int i
=
0
; i
< PI
;
++i
)
-
{
-
flag
=
false
;
-
for
(
int j
=
1
; j
<=
5
; j
++
)
-
{
-
if
(
!curr
[j
].
test
(i
)
)
-
{
-
flag
=
true
;
-
break
;
-
}
-
}
-
if
(
!flag
)
-
{
-
fout
<< angle
<< endl
;
-
return
0
;
-
}
-
}
-
-
for
(
int i
=
1
; i
<=
5
; i
++
)
-
{
-
for
(
int j
=
0
; j
< PI
; j
++
)
-
next
[i
]
[j
]
= curr
[i
]
[
(j
+PI
-speed
[i
]
)
%PI
]
;
-
curr
[i
]
=next
[i
]
;
-
}
-
-
++angle
;
-
}
-
-
fout
<<
"none"
<< endl
;
-
return
0
;
-
}