1355. Bald Spot Revisited
Time limit: 1.0 second
Memory limit: 64 MB
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 ≤ a, b ≤ 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
input | output |
---|---|
5 |
0 |
题意:一个学生梦到自己在一条有很多酒吧的街上散步。他可以在每个酒吧喝一杯酒。所有的酒吧有一个正整数编号,这个人可以从n号酒吧走到编号能整除n的酒吧。现在他要从a号酒吧走到b号,请问最多能喝到多少酒。题目中保证所有数据均为质数之积。
思路:
直接暴力就可以;
第一:如果由a可以到达b,那么a一定可以整除b;
第二:将b除以a后的结果进行素数划分就可以,计算划分出了多少个数就可以计算出最多所喝酒的次数
#include<iostream>
#include<cmath>
#include<map>
#include<cstdio>
using namespace std;
long long a,b;
int sum(long long n)
{
int sumnumber=;
while(){
int i;
for(i=;i*i<=n;i++){
if(n%i==){
while(n%i==){
sumnumber++;
n/=i;
break;
}
break;
}
}
if(i*i>n){
if(n>){
sumnumber++;
}
break;
}
}
return sumnumber;
} int main()
{
// freopen("input.txt","r",stdin);
int n;
scanf("%d",&n);
while(n--){
cin>>a>>b;
if(b%a!=){//b不可以被a整除,表明无论如何由a都不可能到达b的
printf("0\n");
continue;
}
if(b==a){//a等于b那么不用动就可以的
printf("1\n");
continue;
}
if(a>b){//a大于b这样一定不可以由a到达b的
printf("0\n");
continue;
}
int ans=+sum(b/a);//计算b除以a的素数划分
printf("%d\n",ans);
}
return ;
}