Ural 1355. Bald Spot Revisited 质因数分解

来源:互联网 发布:网络广告公司经营项目 编辑:程序博客网 时间:2024/05/20 06:27

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
530 892 163 2431 12 2
04511
Problem Author: Aleksandr Bikbaev
Problem Source: USU Junior Championship March'2005
题目中保证所有数据均为质数之积,这样就不用再素数测试了,直接暴力就行了。
#include <iostream>#include <cstdio>using namespace std;int beer(int t){    int i;    for(i = 2; i*i <= t; i ++)    {        if(t%i == 0) return 1+beer(t/i);    }    return 0;}int main(){    int a, b, ncase, ans;    scanf("%d", &ncase);    while(ncase --)    {        scanf("%d%d", &a, &b);        if(a > b)        {            printf("0\n");            continue;        }        if(a == b)        {            printf("1\n");            continue;        }        ans = 0;        if(b%a == 0)            ans = 2+beer(b/a);        printf("%d\n", ans);    }    return 0;}


原创粉丝点击