UVa 10780 - Again Prime? No Time.
2015-09-18 18:26
399 查看
題目:有兩個數字n,m,輸出最大的k使得m^k能被n!整除。
分析:數論。將m因式分解,統計每個素數因子的次數記為p(0),p(1),...;
再將n!因式分解,求出對應質因數的個數q(0),q(1),...;
则结果为:min(q(0)/p(0),q(1)/p(1),...);
統計n!素數因子p的個數為:n/p + n/p/p + ...(按余数分类即可求出p的所有倍数);
說明:注意不要使用while(scanf()!= EOF),会WA,╮(╯▽╰)╭。
分析:數論。將m因式分解,統計每個素數因子的次數記為p(0),p(1),...;
再將n!因式分解,求出對應質因數的個數q(0),q(1),...;
则结果为:min(q(0)/p(0),q(1)/p(1),...);
統計n!素數因子p的個數為:n/p + n/p/p + ...(按余数分类即可求出p的所有倍数);
說明:注意不要使用while(scanf()!= EOF),会WA,╮(╯▽╰)╭。
#include <cstring> #include <cstdio> int visit[10001]; int prime[10001]; int main() { memset(visit, 0, sizeof(visit)); int count = 0; for (int i = 2; i < 10001; ++ i) if (!visit[i]) { prime[count ++] = i; for (int j = i*i; j < 10001; j += i) visit[j] = 1; } int k, m, n; scanf("%d",&k); for (int t = 1; t <= k; ++ t){ scanf("%d%d",&m, &n); int min = 0x3f3f3f3f; for (int i = 0; m > 1 && i < count; ++ i) { int size = 0; while (m%prime[i] == 0) { m /= prime[i]; ++ size; } if (size) { int now = n, max = 0; while (now) max += now /= prime[i]; if (min > max/size) min = max/size; } } printf("Case %d:\n",t); if (min && min != 0x3f3f3f3f) printf("%d\n",min); else printf("Impossible to divide\n"); } return 0; }
相关文章推荐
- failed to install sourceTree in win7
- leetcode: (70) Climbing Stairs
- LeetCode -- Contains Duplicate
- WebSessionStore: Could not obtain reference to HttpContext
- LeetCode---Submission Details
- 设计模式之职责链模式(Chain Of Responsibility)
- Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536
- nonatomic, retain,weak,strong用法详解
- Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE
- GTS Test fail testTestTelephonyCall_bindsToInCallServiceAndAddsCall
- boost的安装问题 http://blog.csdn.net/begtostudy/article/details/1879213
- [Leetcode] Container With Most Water
- Learning to rank的讲解,单文档方法(Pointwise),文档对方法(Pairwise),文档列表方法(Listwise)
- [Leetcode] 11 Container With Most Water
- Codeforces Round #319 (Div. 1)E.Painting Edges(并查集)
- Contains Duplicate II
- HDU 4819 Mosaic D区段树
- Contains Duplicate
- 添加第三方类库造成的linker command failed with exit code 1 (use -v to see invocation)的错误调试
- Magic Pairs - SGU 119(同余)