UVa 10780-Again Prime? No Time.

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

求N!中M的最高次幂。

思路:先将M质数分解,然后对M的每个素数因子求N!的最高次幂。例如M = 45 N = 67. 45 = 3 ^ 2 * 5 ^ 1. 45的素数因子为3 和 5,             而67!中3的最高次幂为31,5的最高次幂为15.这样就可以确定67!中45的最高幂为15,因为67!中每2个3,1个5就包含                 一个45,换句话说也就是求67!中包含了多少(3^2*5^1)这样的组合,也就是求min ( 31 / 2, 15 / 1 )。

/*************************************************************************
    > File Name: 10780.cpp
    > Author: Toy
    > Mail: ycsgldy@163.com 
    > Created Time: 2013年06月03日 星期一 18时29分03秒
 ************************************************************************/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>

using namespace std;
const int INF = 0x7fffffff;
typedef pair<int,int> II;
typedef vector<int> IV;
typedef vector<II> IIV;
typedef vector<bool> BV;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned int u32;
#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
#define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31)))
#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))
const int MAXP = 46341; //sqrt(2^31)
const int SQRP = 216; //sqrt(MAX)
int _c[(MAXP>>6)+1];
IV primes;
IIV opt;
int Case, m, n;

void prime_sieve ( ) {
	for ( int i = 3; i <= SQRP; i += 2 ) 
		if ( !IsComp ( i ) ) for ( int j = i * i; j <= MAXP; j += i + i ) SetComp ( j );
	primes.push_back ( 2 );
	for ( int i = 3; i <= MAXP; i += 2 ) 
		if ( !IsComp ( i ) ) primes.push_back ( i );
}

void prime_factorize ( int n, IIV &f ) {
	f.clear();
	int sn = sqrt ( n );
	For ( IV, it, primes ) {
		int prime = *it;
		if ( prime > sn ) break;
		if ( n % prime ) continue;
		int e = 0; for ( ; n % prime == 0; e++, n /= prime ) ;
		f.push_back ( II ( prime, e ) );
		sn = sqrt ( n );
	}
	if ( n > 1 ) f.push_back ( II ( n, 1 ) );
}

int get_powers ( int n, int p ) {
	int res = 0;
	for ( int power = p; power <= n; power *= p ) res += n / power;
	return res;
}

int main ( ) {
	prime_sieve ();
	scanf ( "%d", &Case );
	for ( int cnt = 1; cnt <= Case; ++cnt ) {
		int ans = INF;
		scanf ( "%d%d", &m, &n );
		prime_factorize ( m, opt );
		For ( IIV, it, opt ) {
			int tmp = get_powers ( n, it -> first );
			tmp /= it -> second;
			if ( tmp < ans ) ans = tmp;
		}
		printf ( "Case %d:\n", cnt );
		if ( ans == 0 ) printf ( "Impossible to divide\n" );
		else printf ( "%d\n", ans );
	}
    return 0;
}


UVA 10780-Again Prime? No Time. (数学-质因子) 题目 阅读详情

相关推荐

uva 10780 Again Prime? No Time.

uva 10780 Again Prime? No Time.

@fei 617

UVa:10780 Again Prime? No Time.

入门题啊。。总算是让我给想出来了。 先对m分解质因数(这里要注意时间复杂度是根号m,循环外面还有一部分),然后对于枚举1到n,分别去除尽m的每个质因数,统计每个质因数被整除的次数。 最后算m一共被整除多少次即可。   #include #include #include #include #include #include #include #define MA

Fighting 726

uva 10780 Again Prime? No Time. 质因子乱搞

求最大的k   使得 m^k 能被n!整除 m^k就是让m的每个质因子个数增加了k倍,只要求得n!的质因子能让m增加多少倍就够了。当然这里要取增加倍数最少的。 木桶装水的量取决于最短的木板。 预处理2-n每个数的质因子情况,由于n有10000,前10000个素数有1000+个,所以维护前缀和不划算。 case只有500 所以干脆每次都算一遍。 #include #include #inc

792

Again Prime? No Time.UVA 10780

预处理答案ans为+∞,定义函数ok(x,y)表示y!中含有的x的最高次幂的指数,定义函数go(y,x)表示y中含有的x的最高次幂的指数,将m分解质因数,对于每一个质因数i,当前答案now=ok(i,n)/go(m,i),假如now更小则更新ans。 求y!中x的最高次幂的方法为求出并加上1到y中能被x整除的数的个数即y/x,然后将每个数除以x,不能整除的数删去,剩下的数就变成了1到y/x,重复该操作直到y==0。

TryMyEdge 486

uva10780 - Again Prime? No time

uva10780 - Again Prime? No time    Again Prime? No time. 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!.  

坚持不懈 567

UVA10780 Again Prime? No Time.【筛选法】

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 firs...

海岛Blog 421

uva 10780 - Again Prime? No Time.(数论)

题目链接:uva 10780 - Again Prime? No Time. 题目大意:给定m和n,计算n!中有多少个因子m。 解题思路:将1~n分解质因子,然后在将m分解,对应每个因子计算说可以保证的个数,维护最小值。 #include #include #include #define min(a,b) (a)<(b)?(a):(b) const i

不慌不忙、不急不躁 2200

UVA 10780 - Again Prime No Time

UVA 10780 - AgainPrime No Time   原题大意:已知m和n,求最大的k ,使(m^k) | n!。 分析:我们知道,对于a=a1^p1*a2^p2*……an^pn  ,  b=b1^q1*b2^q2*……bn^qm, 若a | b,则 a的质因子集A={a1,a2,…..,an}必定是b的质因子集B={b1,b2,….,bm}的子集,且对于所有ai=bj有pi

u011276914的专栏 791

UVa 10780 - Again Prime? No Time

Description 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 tes...

weixin_30824599的博客 97

UVA 10780 Again Prime? No time

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 ne

yichin的专栏 536

UVa 10780 - Again Prime? No Time.

題目:有兩個數字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 +

小白菜又菜 1408

UVA 10780-Again Prime? No Time.

题目链接题目解析题意输入数据组数t,每组数据包含2个整数m和n,若m^k能被n!整除,输出最大的K,否则输出”Impossible to divide”。思路将m分解质因数,求出每个质因数的个数p[i],在n!中相对应m中质因数的个数为q[i],q[i]÷p[i]的最小值即为所求的K的值。 如何求m和n!的质因数见代码

XuKathy的博客 435

Uva10780 Again Prime? No Time.

这题搞了1个多小时= =!靠着UVA的BBS上的数据才过的 思路是:把m质因数分解,各质因数的系数用map(a)记录,把1到n的数都质因数分解,用map(b)记录(累加),然后逐一遍历,碰到a[i]=0则跳过,碰到b[i] = 0& a[i]!= 0 则无解,接下来就是我无法理解地方了,判断前两种情况之后,如果碰到b[i] 无法整除a[i] 的时候,理应是无解,但是我只要这样判 就是WA,如果不判

mowayao的专栏 797

UVA 10780 Again Prime? No Time. [质因子分解]【数论】

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=120197#problem/HThe problem statement is very easy. Given a number n you have to determine the largest power of m, not necessarily prime, tha

Tabris的博客 494

UVa 10780Again Prime? No time.

【题目大意】 给定两个正整数N,M。求一个最大的正整数k,使得Mk|N! 【分析】 直接将N!和M的质因子分解,然后比较指数即可。 N!的质因子分解式:     设N!=p1a1 × p2a2 × p3a3 ××× pkak 那么pi的指数 ai=sigma(N/(pij))  pij 【代码】 #include #include #include #inc

Ciocio 918

UVA 10780 Again Prime No Time.(数学)

给定两个整数m和n,求最大的k使得m^k是n!的约数 对m质因子分解,然后使用勒让德定理求得n!包含的质数p的阶数,min(b[i] / a[i])即为结果k, 若为0无解 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring>...

Nobel7889的专栏 155

[UVa 10780]Again Prime?No time.

数学

_Horizon 602

UVA10780Again Prime? No Time.

题意:给定m和n,找到最大的ans,使得m的ans次幂是n!的约数。 思路:瞎做。质因数分解,将m进行质因数分解,再对n!进行质因数分解。设prime[]为素数表。 设pi为将m质因数分解后prime[i]的number。设qi为将n!进行质因数分解后prime[i]的number。那么 ans=min(qi/pi) 代码:#include <iostream> #include <cstd

inkysakura's blog 224

Again Prime? No Time.---UVA10780幂和阶乘(唯一分解)

UVA - 10780 题目链接https://vjudge.net/contest/321024#problem/D 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!...

孤风的博客 287
上一篇: UVa 10622 - Perfect P-th Powers
下一篇: UVa 10738 - Riemann vs Mertens
AcToy
博客等级 码龄14年 12粉丝 172原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值