Again Prime? No Time.(UVA 10780)

Again Prime? No Time. http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31675#problem/D 涨姿势了。。。 // File Name: uva10780.cpp // Author: bo_jwolf // Created Time: 2013年09月16日 星期一 21:54:27 #include #include #include # 阅读详情

题目链接
预处理答案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。
代码实现:

int ok(int x,int y)
{
    int ans=0;
    while(y)
    {
        ans+=y/x;
        y/=x;
    }
    return ans;
}

附上AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<cmath>
#define pq priority_queue
#define Pi acos(-1.0)
#define MAXX 1000000007
using namespace std;
bool life[6666];
int v[6666],l=0;
int ok(int x,int y)
{
    int ans=0;
    while(y)
    {
        ans+=y/x;
        y/=x;
    }
    return ans;
}
int go(int y,int x)
{
    int ans=0;
    while(y%x==0)
    {
        ans++;
        y/=x;
    }
    return ans;
}
int main()
{
    for(int j=2;j<=5000;j++)
    {
        if(!life[j])
        {
            v[l++]=j;
            for(int k=2;k*j<=5000;k++)
                life[k*j]=1;
        }
    }
    int t,n,m,i=0,now;
    int ans;
    cin>>t;
    while(i<t)
    {
        i++;
        ans=100000000;
        scanf("%d%d",&m,&n);
        cout<<"Case "<<i<<":"<<endl;
        for(int j=0;j<l;j++)
        {
            if(m%v[j]==0)
            {
                ans=min(ans,ok(v[j],n)/go(m,v[j]));
            }
        }
        if(ans)
            cout<<ans<<endl;
        else
            cout<<"Impossible to divide"<<endl;
    }
    return 0;
}

Memory: 0 KB Time: 0 MS
Language: C++ 4.8.2 Result: Accepted

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 阅读详情

相关推荐

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.

分解质因数处理。 用

Hardbird 768

uva 10780Again 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

1690

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

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

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

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

uva 10780 Again Prime? No Time.

uva 10780 Again Prime? No Time.

@fei 617

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.

求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,换句话说也就是

AcToy 1032

[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
上一篇: How do you add?(UVA 10943)
下一篇: LCM Cardinality(UVA 10892)
TryMyEdge
博客等级 码龄10年 0粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值