SGU - 154 - Factorial (数论)
2015-03-12 19:58
330 查看
154. Factorial
time limit per test: 0.25 sec.memory limit per test: 4096 KB
input: standard input
output: standard output
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.
Input
One number Q written in the input (0<=Q<=10^8).
Output
Write "No solution", if there is no such number N, and N otherwise.
Sample test(s)
Input
2
Output
10
[submit]
[forum]
Author: | Andrew V. Lazarev |
Resource: | Saratov Subregional School Team Contest, 2002 |
Date: | Spring, 2002 |
质因数5的个数即可,因为2的个数会比5多很多,所以质因数有多少5,尾部就有多少0........
有:
Q = N/5 + N/(5^2) + N/(5^3) + ...
由等比数列求和可得(设只到前k项):
Q = N(5^k - 1) / [4*(5^k)],由此得:
N = 4Q * [(5^k)/(5^k-1)]
注意:当Q为0时要输出1
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int fun(int x) { //求x尾部有多少个0 int sum = 0; while(x) { sum += x / 5; x /= 5; } return sum; } int main() { int Q; while(scanf("%d", &Q) != EOF) { if(Q == 0) { printf("1\n"); continue; } int N = Q * 4 / 5 * 5; //依据公式先大致算到4*Q(除5乘5是为了N正好是5的倍数),然后再往后推 while(fun(N) < Q) { N += 5; } if(fun(N) == Q) printf("%d\n", N); else printf("No solution\n"); } return 0; }
相关文章推荐
- SGU 154 Factorial(数论+二分)
- SGU154——Factorial (poj1401变形题——数论+二分)
- SGU154 Factorial(结尾有多少0)
- SGU 154-Factorial
- SGU-154 Factorial (末尾0 & 三分)
- sgu 154 Factorial
- SGU - 154 Factorial(阶乘素因子次数的逆问题)
- sgu154(数论入门)
- sgu154:Factorial
- SGU 154. Factorial
- 最小N使得N!的末尾恰有Q个0——SGU-154 Factorial
- SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3
- SPOJ 5161 FACVSPOW - Factorial vs Power(数论+二分)
- sgu——261(数论之N次剩余问题)
- SGU 118 Digital Root(数论)
- 【luogu10月月赛】一道中档题 Factorial(数论)
- HDU 1124 Factorial(数论)
- HDU 1124 Factorial (数论)
- SGU 105 Div 3(数论)
- HDU 11124 Factorial (数论)