Again Prime? No Time.(uva10870+数论)
Again Prime? No time.
Input: standard input
Output: standard output
Time Limit: 1 second
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
that500 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
题意:求N!%M^k==0;最大的k;
思路:先算M的质因子m,并保存质因子的个数,从N开始递减到N/m,计算每个质因子在N!中出现的次数,取最小。
一开始我的思路是取最大的质因子,然后发现错了,然后想是质因子*个数最大的那个,然后想了下40,100就不对了。所以就把所有的质因子都算了一次。(本来以为会超时。。。结果142ms过。o(︶︿︶)o
唉)
转载请注明出处:寻找&星空の孩子
题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=77956#problem/E
#include<stdio.h>
#include<string.h>
#define LL long long
/*
bool prime(int x)
{
for(int i=2;i*i<=x;i++)
{
if(!(x%i)) return 0;
}
return 1;
}*/
LL a[];
inline void Maxprime(LL x)
{
LL t=,k=;
for(LL i=; i<=x; i++)
{
while(x%i==)
{
t=i;
a[i]++;
x/=i;
}
}
if(t==)
{
a[x]=;
}
}
inline LL Ssum(LL x)
{
return (x*(x+))/;
}
LL Sumprime(LL x,LL mod)
{
LL t=;
while(x)
{
if(x%mod==) t++,x/=mod;
else return t;
}
return ;
} int main()
{
LL n,m,T,caseT=;
scanf("%lld",&T);
while(caseT<=T)
{
memset(a,,sizeof(a));
// memset(b,0,sizeof(b));
scanf("%lld%lld",&m,&n);
LL mod;
Maxprime(m);
/* if(mod!=m)
{
LL tt=0;
for(LL i=2;i<n;i++)
{
if(tt<i*a[i])
{
tt=i*a[i];
mod=i;
}
}
}*/
// printf("mod=%d\n",mod); LL sum,minsum=;
for(LL j=; j<=m; j++)
{ if(!a[j]) continue;
sum=;
for(LL i=n; i>n/j; i--)
{
LL tmp=Sumprime(i,j);
if(tmp) sum+=Ssum(tmp);
}
if(minsum>sum/a[j]) minsum=sum/a[j];
}
printf("Case %lld:\n",caseT++);
if(!sum) printf("Impossible to divide\n");
else printf("%lld\n",minsum);
}
return ;
}
Again Prime? No Time.(uva10870+数论)的更多相关文章
- UVA10140 Prime Distance【素数/数论】By cellur925
题目传送门 我们注意到,L,R是肥肠大的.........我们不可能在1s内筛出2^31内的全部质数. “上帝为你关上一扇门,同时为你打开一扇窗” 我们又注意到,R-L是肥肠比较小的,珂以从这入手解决 ...
- 【HDU】2866:Special Prime【数论】
Special Prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- POJ 1365 Prime Land(数论)
题目链接: 传送门 Prime Land Time Limit: 1000MS Memory Limit: 10000K Description Everybody in the Prime ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- UVA 11610 Reverse Prime (数论+树状数组+二分,难题)
参考链接http://blog.csdn.net/acm_cxlove/article/details/8264290http://blog.csdn.net/w00w12l/article/deta ...
- codeforces 680C C. Bear and Prime 100(数论)
题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...
- 【斐波拉契+数论+同余】【ZOJ3707】Calculate Prime S
题目大意: S[n] 表示 集合{1,2,3,4,5.......n} 不存在连续元素的子集个数 Prime S 表示S[n]与之前的所有S[i]互质; 问 找到大于第K个PrimeS 能整除X 的第 ...
- UVA 10140 - Prime Distance(数论)
10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...
随机推荐
- 关于浏览器跨域的一种实现--jsonp
最近一直在搞python,前端技术荒废很久了,今天跟前端联调,设计到一个前端跨域的问题:前端人员告诉我可以用jsonp的方式实现,经他这么一提醒,也是豁然开朗. jsonp的实现方式我按照我的理解说一 ...
- C/C++ 多线程机制
一.C/C++多线程操作说明 C/C++多线程基本操作如下: 1. 线程的建立结束 2. 线程的互斥和同步 3. 使用信号量控制线程 4. 线程的基本属性配置 在C/C++代码编写时,使用多线程机制, ...
- Android精通之Handler讲解
版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...
- 利用 Linux tap/tun 虚拟设备写一个 ICMP echo 程序
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 前面两篇文章已 ...
- Vue 中是如何解析 template 字符串为 VNode 的?
在接触 React 时候,我只了解到通过 babel 可以把 JSX 转成 VNode(通过调用 React.createElement 方法),但是对其具体是如何转换的却不了解. 很明显,回答失败. ...
- 原生js的简单倒计时
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>倒计 ...
- Java核心技术及面试指南 JDBC部分的面试题总结以及答案
5.5.1 你最近的项目里用到的是哪个数据?或你用过哪些数据库?或你对哪个数据库最熟悉? 通过这个问题,我们将会确认候选人是否在项目里用过数据库或JDBC. 5.5.2 你有没有建过表?或修改表里的字 ...
- vue 项目实战 (vue全家桶之--- vuex)
老规矩先安装 npm install vuex --save 在看下面内容之前 你应该大概的看了一边vuex官方的文档对vuex有个大概对了解 首先 vuex 是什么? vuex 是属于vue中的什么 ...
- visual Studio 2017 扩展开发(三)《绑定快捷键到菜单项》
如何将键盘快捷方式映射到自定义按钮,怎么使用快捷键启动自己创建的菜单,刚开始做的时候迷糊了,找了很久.可能也是因为刚开始做不是很明白,后面慢慢就懂了.其实非常简单的. 很多快捷键已经在Visual s ...
- 聊聊JVM(二)说说GC的一些常见概念
转自CSDN 上一篇总结GC的基础算法,各种GC收集器的基本原理,还是比较粗粒度的概念.这篇会整理一些GC的常见概念,理解了这些概念,相信对GC有更加深入的理解 1. 什么时候会触发Minor GC? ...