2021牛客寒假算法基础集训营4 J 数论 gcd 求 大幂次 序列 的gcd 质因数分解

题目

在这里插入图片描述

题解思路

将每个底数质因数分解 ,取每个质因数的最低幂次(乘之前的幂次)即可。
最后gcd出底数序列的最大公约数。
再将最大公约数分解质因数。
通过预处理的每个质因子的幂次运用快速幂相乘即可。
在这里插入图片描述

AC代码
#include <bits/stdc++.h>
//#include <unordered_map>
//priority_queue
#define PII pair<int,int>
#define ll long long

using namespace std;

const  int  INF =  0x3f3f3f3f;
const  int N = 10010;
const  int mod = 1e9 + 7 ; 
long long a[N] ;
long long p[N] ; 
long long vis[N] ; 

long long ksm( long long di , long long mi )
{
    long long res = 1 ;
    while (mi)
    {
        if (mi & 1 )
            res = res * di % mod ; 
        mi >>= 1 ; 
        di = di * di % mod ; 
    }
    return res ; 
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n ;
    cin >> n ;
    for (int i = 1 ; i <= n ; i++ )
        cin >> a[i] ; 
    for (int i = 1 ; i <= n ; i++ )
        cin >> p[i] ; 
    if  ( n == 1 )
    {
        cout << ksm(a[1],p[1])<<"\n" ; 
    }else
    {
        unordered_map <long long , long long > mp ; 
        long long res = -1 ; 
        for (int i = 1 ; i <= n ; i++ )
        {
            long long st = a[i] ; 
            for (long long j  = 2 ; j * j <= st ; j++ )
            {
                if ( st % j == 0 )
                {
                    long long  cnt = 0 ;
                    while ( st % j == 0 )
                    {
                        st /= j ;
                        cnt++;
                    }
                    if ( mp[j] == 0 )
                    {
                        mp[j] = p[i]*cnt ; 
                    }else
                    {
                        mp[j] = min( mp[j] , p[i]*cnt ) ; 
                    }
                }
            }
            if ( st != 1 )
            {
                if (mp[st] == 0 )
                    mp[st] = p[i] ;
                else
                    mp[st] = min( p[i] , mp[st] ) ; 
            }
        }
        for (int i = 1 ; i <= n ; i++ )
        {
            if (res == -1 )
                res = a[i] ; 
            else
                res = __gcd(res , a[i] ) ;
        }
        //cout << res << "\n" ;
        long long ans = 1 ; 
        for (long long i = 2 ; i*i <= res ; i++ )
        {
            if ( res % i == 0 )
            {
                while ( res % i == 0 )
                    res /= i ;
                ans = ans * ksm(i,mp[i]) % mod ; 
            }
        }
        if ( res != 1 )
            ans = ans * ksm(res,mp[res]) % mod ; 
        cout << ans << "\n" ; 
    }
    return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值