uva10780Again Prime? No Time.

质因数分解。

 1 //Accepted    0 KB    12 ms
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 const int MAXN = 100005;
6 const int inf = 100000000;
7 int pri[MAXN];
8 int cnt;
9 void prime()
10 {
11 cnt=0;
12 for (int i=2;i<MAXN;i++)
13 {
14 if (!pri[i])
15 {
16 pri[cnt++]=i;
17 if ((long long )i*i<(long long )MAXN)
18 for (int j=i*i;j<MAXN;j+=i)
19 pri[j]=1;
20 }
21 }
22 }
23 int p[100],pNum[100];
24 int num;
25 void getp(int n)
26 {
27 num=0;
28 memset(pNum,0,sizeof(pNum));
29 for (int i=0;i<cnt && pri[i]*pri[i]<=n;i++)
30 {
31 if (n%pri[i]==0)
32 {
33 while (n%pri[i]==0)
34 {
35 n/=pri[i];
36 pNum[num]++;
37 }
38 p[num++]=pri[i];
39 }
40 }
41 if (n!=1)
42 {
43 pNum[num]=1;
44 p[num++]=n;
45 }
46 }
47 int getn(int n,int m)
48 {
49 int ans=0;
50 while (n>0)
51 {
52 ans+=n/m;
53 n/=m;
54 }
55 return ans;
56 }
57 int min(int a,int b)
58 {
59 return a>b?b:a;
60 }
61 int slove(int n,int m)
62 {
63 getp(m);
64 int ans=inf;
65 for (int i=0;i<num;i++)
66 {
67 ans=min(ans,getn(n,p[i])/pNum[i]);
68 }
69 return ans;
70 }
71 int n,m;
72 int main()
73 {
74 prime();
75 int T;
76 scanf("%d",&T);
77 for (int t=1;t<=T;t++)
78 {
79 scanf("%d%d",&m,&n);
80 int ans=slove(n,m);
81 printf("Case %d:\n",t);
82 if (ans==0)
83 printf("Impossible to divide\n");
84 else
85 printf("%d\n",ans);
86 }
87 return 0;
88 }

时间: 2024-09-30 08:18:49

uva10780Again Prime? No Time.的相关文章

Sicily 1444: Prime Path(BFS)

题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 bool isPrime(int n){//素数判断 5 if(n == 2 || n == 3) return true; 6 else{ 7 int k = sqrt(n) + 1; 8 for(int i = 2; i < k; i

HDU 1389 继续畅通工程【最小生成树,Prime算法+Kruskal算法】

继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21871    Accepted Submission(s): 9356 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列

HDU 1016 Prime Ring Problem(DFS)

题目链接 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always

[CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟之前LeetCode的那道Ugly Number II 丑陋数之二基本没有啥区别,具体讲解可参见那篇,代码如下: class Solution { public: int getKthMagicNumber(int k) { vector<int> res(1, 1); int i3 = 0, i

UVA 10200 Prime Time 暴力水题

一个简单的暴力水题,只是输出方式让人无语... #include <stdio.h> #include <string.h> int prime(int n) { int i; for(i=2;i*i<=n;i++) { if((n%i)==0) return 0; } return 1; } int main() { int num[10010]; int i; int a,b; int sum; memset(num,0,sizeof(num)); for(i=0;i&l

POJ 2689 Prime Distance(素数区间筛法--经典题)

大致题意:给定[L,R]区间,找出区间内的每个素数 数据范围 : 1<=L< R<=2,147,483,647) R-L <=1,000,000. R的数值太大,所以不能直接筛[0,R]的,要空间和时间优化,用到区间筛法,另外注意不能用int,因为R和L都是满int的,中间有很多细节处理会爆int的,还要注意1不是素数,所以在区间筛中要特判一下,是个易错的地方 //1160K 16MS C++ 1539B #include<cstdio> #include<ios

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

07:清泉-改(prime+堆)

时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  512000kB 描述 华北电力大学可以抽象为一张有n个点m条边的无向图. 现在所有的边都断了. 修复每条边都有个不同的代价w_i. 求让所有点都能互相到达的最小代价和. 输入 第一行两个正整数 n, m 表示顶点数和边数 接下来m行每行三个正整数 u v w 表示一条边 (u和v是边的端点, w是边权) 输出 输出一行一个正整数表示答案 样例输入 2 2 1 2 2 2 1 3 样例输出 2 提示 n ≤ 10^

Sum of Consecutive Prime Numbers POJ - 2739

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The intege