ural 1355. Bald Spot Revisited(数的素因子划分)

1355. Bald Spot Revisited

Time limit: 1.0 second
Memory limit: 64 MB
A student dreamt that he walked along the town where there were lots of pubs. He drank a mug of ale in each pub. All the pubs were numbered with positive integers and one could pass from the pub number n to the pub with a number that divides n. The dream started in the pub number a. The student knew that he needed to get to the pub number b. It’s understood that he wanted to drink on the way as much ale as possible. If he couldn’t get from the pub number a to the pub number b he woke up immediately in a cold sweat.

Input

The first line contains an integer T — an amount of tests. Then T lines with integers a and bfollow (0 ≤ T ≤ 20; 1 ≤ ab ≤ 109).

Output

For each test in a separate line you are to output the maximal number of mugs that the student could drink on his way.

Sample

inputoutput
5
30 89
2 16
3 243
1 1
2 2
0
4
5
1
1

题意:一个学生梦到自己在一条有很多酒吧的街上散步。他可以在每个酒吧喝一杯酒。所有的酒吧有一个正整数编号,这个人可以从n号酒吧走到编号能整除n的酒吧。现在他要从a号酒吧走到b号,请问最多能喝到多少酒。题目中保证所有数据均为质数之积。

思路:

直接暴力就可以;

第一:如果由a可以到达b,那么a一定可以整除b;

第二:将b除以a后的结果进行素数划分就可以,计算划分出了多少个数就可以计算出最多所喝酒的次数

 1 #include<iostream>
 2 #include<cmath>
 3 #include<map>
 4 #include<cstdio>
 5 using namespace std;
 6 long long a,b;
 7 int sum(long long n)
 8 {
 9     int sumnumber=0;
10     while(1){
11         int i;
12         for(i=2;i*i<=n;i++){
13             if(n%i==0){
14                 while(n%i==0){
15                     sumnumber++;
16                     n/=i;
17                     break;
18                 }
19                 break;
20             }
21         }
22         if(i*i>n){
23             if(n>1){
24                 sumnumber++;
25             }
26             break;
27         }
28     }
29     return sumnumber;
30 }
31 
32 int main()
33 {
34 //    freopen("input.txt","r",stdin);
35     int n;
36     scanf("%d",&n);
37     while(n--){
38         cin>>a>>b;
39         if(b%a!=0){//b不可以被a整除,表明无论如何由a都不可能到达b的
40             printf("0
");
41             continue;
42         }
43         if(b==a){//a等于b那么不用动就可以的
44             printf("1
");
45             continue;
46         }
47         if(a>b){//a大于b这样一定不可以由a到达b的
48             printf("0
");
49             continue;
50         }
51         int ans=1+sum(b/a);//计算b除以a的素数划分
52         printf("%d
",ans);
53     }
54     return 0;
55 }
View Code
原文地址:https://www.cnblogs.com/zhangchengbing/p/3413220.html