Again Prime? No Time.(UVA 10780)
2016-07-18 23:49
447 查看
题目链接
预处理答案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。
代码实现:
附上AC代码:
Memory: 0 KB Time: 0 MS
Language: C++ 4.8.2 Result: Accepted
预处理答案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
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?