Kattis之旅——Factovisors
The factorial function, n! is defined thus for n a non-negative integer:
We say that a divides b if there exists an integer k such that
0! = 1
n! = n * (n-1)! (n > 0)
We say that a divides b if there exists an integer k such that
k*a = b
Input
The input to your program consists of several lines, each containing two non-negative integers, n and m, both less than 2^31.
Output
For each input line, output a line stating whether or not m divides n!, in the format shown below.
Sample Input
6 9 6 27 20 10000 20 100000 1000 1009
Sample Output
9 divides 6! 27 does not divide 6! 10000 divides 20! 100000 does not divide 20! 1009 does not divide 1000!
给一个整数n和一个数m,求n的阶乘是否可以整除m。
将m分解质因数,然后对每个质因数在n中进行判断是否含有相同或者更多。
//Asimple #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 76000; ll n, m, s, res, ans, len, T, k, num; int x, y; int pr[maxn]; void get_pr(){ int a[maxn] = {0}; len = 0; for(int i=2; i<maxn; i++) { if( a[i]==0 ) { pr[len++] = i; int j = i; while( j < maxn ) { a[j] = 1; j += i; } } } } bool judge(int x, int cnt) { int c = 0; ll t = n; while( t ) { c += t/x; t /= x; } return cnt<=c; } void input() { get_pr(); while( cin >> n >> m ) { if( m == 0 ) { printf("%lld does not divide %lld!\n",m,n); continue; } ll t = m; bool f = true; for(int i=0; i<len && pr[i]<=m; i++) { if( m%pr[i] == 0 ) { res = 0; while( m%pr[i]==0 ) { res ++; m /= pr[i]; } f = f&&judge(pr[i], res); } } if( m!=1 ) f = f&&judge(m, 1); if( f ) printf("%lld divides %lld!\n",t,n); else printf("%lld does not divide %lld!\n",t,n); } } int main(){ input(); return 0; }
低调做人,高调做事。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理