UVa 10780 Again Prime? No Time. (数论&素因子分解)
2013-09-08 13:27
507 查看
10780 - Again Prime? No Time.
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=1721
The problem statement is very easy. Given a number n you have to determine the largest power of m, not necessarily prime, that divides n!.
Input
The input file consists of several test cases. The first line in the file is the number of cases to handle. The following lines are the cases each of which contains two integers m (1<m<5000) and n
(0<n<10000). The integers are separated by an space. There will be no invalid cases given and there are not more that 500 test cases.
Output
For each case in the input, print the case number and result in separate lines. The result is either an integer if m divides n! or a line "Impossible
to divide" (without the quotes). Check the sample input and output format.
Sample Input
2
2 10
2 100
Sample Output
Case 1:
8
Case 2:
97
完整代码:
/*0.012s*/ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 5010; const int INF = 10000; int prime[671], cnt; bool vis ; inline void create_prime() { for (int i = 2; i < N; ++i)///生成5010内的素数,多求一点 if (!vis[i]) { prime[cnt++] = i; for (int j = i * i; j <= N; j += i) vis[j] = true; } } inline int count(int n, int prime) { int sum = 0; while (n > 1) { sum += n / prime; n /= prime;///另一种统计方法:改变n的值 } return sum; } inline int index(int m, int prime) { int index = 0; while (m % prime == 0) { ++index; m /= prime; } return index; } int main(void) { create_prime(); int T, m, n, mm; scanf("%d", &T); for (int cas = 1; cas <= T; ++cas) { scanf("%d%d", &m, &n); mm = INF; for (int i = 0; prime[i] <= m; i++) if (m % prime[i] == 0) { mm = min(mm, count(n, prime[i]) / index(m, prime[i]));///核心代码 if (mm == 0) break; } printf("Case %d:\n", cas); if (mm) printf("%d\n", mm); else puts("Impossible to divide"); } return 0; }
相关文章推荐
- UVA 10780 Again Prime? No Time. [质因子分解]【数论】
- UVa 10780 - Again Prime? No Time.(唯一分解)
- UVa 10780 - Again Prime? No time (质因式分解)
- UVA 10780 - Again Prime? No Time.(数论)
- UVA10780:Again Prime? No Time(数论)
- UVA-10780-Again Prime? No Time(素数分解)
- UVA10780 - Again Prime? No Time.(分解质因子)
- UVA 10780 Again Prime? No Time. ——质因分解
- uva 10780 - Again Prime? No Time.(数论)
- Uva 10780 Again Prime? No Time.(分解质因子)
- Again Prime? No Time.(uva10870+数论)
- [UVa 10780]Again Prime?No time.
- Again Prime? No Time.(UVA 10780)
- Again Prime? No Time.(uva10870+数论)
- uva 10780 Again Prime? No Time. 质因子乱搞
- UVa10780 - Again Prime?No time.(质因数分解)
- UVa 10892 LCM Cardinality (数论&素因子分解)
- Uva10780 Again Prime? No Time.
- UVA 10780 - Again Prime? No Time.
- UVA - 10780 Again Prime? No Time. (质因子分解)