您的位置:首页 > 其它

Ural 1355. Bald Spot Revisited 质因数分解

2013-07-31 20:56 357 查看


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 ≤ 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

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

0
4
5
1
1

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: