HDOJ 4497 GCD and LCM

22 篇文章 0 订阅
3 篇文章 0 订阅

题意:求满足G=GCD(x,y,z)&& L=LCM(x,y,z)的(x,y,z)数量。(1,2,3)(1,3,2)视为两种情况。

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497

思路:容斥定理,k=L/G,求出k的为素数的约数的所有可能,最后将结果ans*=6

注意点:当L无法被G整除时,无解。当L=G的时,ans=1。这两种情况需要特判。


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
124307652014-12-08 18:47:47Accepted44970MS1336K1990 BG++luminous11
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;

bool prime[100005];
int num[10000];

int main()
{
    ios::sync_with_stdio( false );
    int m, n;
    int t;
    clr( prime, 1 );
    prime[0] = prime[1] = false;
    for ( int i = 2; i < 100000; i ++ )
    {
        if ( prime[i] )
        {
            for ( int j = 2; j * i < 100000; j ++ )
            {
                prime[i*j] = false;
            }
        }
    }
    int cnt = 0;
    for ( int i = 0; i < 100000; i ++ )
    {
        if ( prime[i] )
        {
            num[cnt++] = i;
        }
    }
    cin >> t;
    while ( t -- )
    {
        cin >> n >> m;
        int ans = 1;
        if ( m % n )
        {
            cout << "0" << endl;
            continue;
        }
        int k = m / n;
        int s = k;
        for ( int i = 0; i < cnt; i ++ )
        {
            int acs = num[i] * num[i];
            if ( acs > s )
            {
                break;
            }
            if ( ! ( k % num[i] ) )
            {
                int tmp = 0;
                while ( ! ( k % num[i] ) )
                {
                    k /= num[i];
                    tmp ++;
                }
                ans *= tmp * 6;
            }
        }
        if ( k != 1 )
        {
            ans *= 6;
        }
        cout << ans << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值