B.Basic GCD Problem[打表][优化最大质因子][优化快速幂]or[素数筛][质因子分解]

As a great ACMer, ZYB is also good at math and number theory. 

ZYB constructs a function fc(x)fc​(x) such that: 

As a great ACMer, ZYB is also good at math and number theory. 

ZYB constructs a function fc(x)fc​(x) such that: 

在这里插入图片描述

Give some positive integer pairs (ni,ci)(ni​,ci​), ZYB wants to know fci(ni)mod  (109+7)fci​​(ni​)mod(109+7).

输入描述:

 

The input contains multiple test cases. The first line of input contains one integer T(1≤T≤106)T(1≤T≤106).

In the following TT lines, each line contains two integers ni,cini​,ci​ (1≤ni,ci≤1061≤ni​,ci​≤106) describing one question.

输出描述:

For each test case, output one integer indicating the answer.

示例1

输入

复制

2
3 3
10 5

输出

复制

3
25

Give some positive integer pairs (ni,ci)(ni​,ci​), ZYB wants to know fci(ni)mod  (109+7)fci​​(ni​)mod(109+7).

输入描述:

 

The input contains multiple test cases. The first line of input contains one integer T(1≤T≤106)T(1≤T≤106).

In the following TT lines, each line contains two integers ni,cini​,ci​ (1≤ni,ci≤1061≤ni​,ci​≤106) describing one question.

输出描述:

For each test case, output one integer indicating the answer.

示例1

输入

复制

2
3 3
10 5

输出

复制

3
25

读了好多遍题才读懂,C^n,n=最大因子

打表质因子

	int arr[N] = {0};//N == 1e6 + 5
    for(int i = 1;i <= N;i++)
    {
        int sum = 0,n = i;
        for(int j = 2;j * j <= n;j++)
            while(n % j == 0)
                sum++,n/=j;
        if(n > 1) sum++;//标准的质因数分解模板,分解出来个数
        arr[i] = sum;
    }

优化打表质因子

	int arr[N] = {0};//N == 1e6 + 5
    for(int i = 1;i <= N;i++)
    {
        int sum = 0,n = i;
        for(int j = 2;j * j <= n;j++)
        {
            while(n % j == 0)
            {
                sum++,n/=j;
                if(arr[n] != 0)//这个数前面已经处理过了,那么就直接加就可以了,不用再判断了
                {
                    arr[i] = arr[n] + sum;
                    n = 0;
                    break;
                }
            }
            if(n == 0) break;
        }
        if(n == 0) continue;
        if(n > 1) sum++;
        arr[i] = sum;
    }

优化快速幂

ll quick_pow(ll a,ll b,ll mod)
{
    ll ans = 1;
    while(b)
    {
        if(b&1)
            ans = a*ans%mod;
        b>>=1;
        a = a*a%mod;
    }
    return ans%mod;
}

ac代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int mod = 1e9+7;
typedef long long ll;
int arr[N],t;
ll quick_pow(ll a,ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b&1)
            ans = a*ans%mod;
        b>>=1;
        a = a*a%mod;
    }
    return ans%mod;
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    for(int i = 1;i <= N;i++)
    {
        int sum = 0,n = i;
        for(int j = 2;j * j <= n;j++)
        {
            while(n % j == 0)
            {
                sum++,n/=j;
                if(arr[n] != 0)
                {
                    arr[i] = arr[n] + sum;n = 0;
                    break;
                }
            }
            if(n == 0) break;
        }
        if(n == 0) continue;
        if(n > 1) sum++;
        arr[i] = sum;
    }
    scanf("%d",&t);
    while(t--)
    {
        ll n,c;
        scanf("%lld %lld",&n,&c);
        printf("%lld\n",quick_pow(c,arr[n]));
    }
}

素数筛

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const ll N=1e6+10;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}
ll prime[N],k,t,n,c;
bool isprime[N];
void Prime(){
    fill(isprime,isprime+N,1);
    k=0;
    prime[1]=0;
    for(ll i=2;i<N;i++){
        if(isprime[i]){
            prime[k++]=i;
            for(ll j=2;i*j<N;j++)
                isprime[i*j]=0;
        }
    }
}

ll solve(ll n){
    ll cnt=0,sum=0;
    for(ll i=0;i<k&&prime[i]*prime[i]<=n;i++){
        if(n%prime[i]==0){
            while(n%prime[i]==0){
                sum++;
                n/=prime[i];
            }
        }
    }
    if(n>1) sum++;
    return sum;
}

int main(){
    Prime();
    scanf("%lld",&t);
    while(t--){
        scanf("%lld%lld",&n,&c);
        printf("%lld\n",power(c,solve(n)));
    }
}

参考

https://blog.csdn.net/moasad/article/details/107463673?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159531859219725211926889%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=159531859219725211926889&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_blog_default-4-107463673.pc_v2_rank_blog_default&utm_term=2020牛客多校第四场

https://zaizai.blog.csdn.net/article/details/107470600?ops_request_misc=&request_id=&biz_id=102&utm_term=2020牛客多校第四场&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-0-107470600

猜你喜欢

转载自blog.csdn.net/qq_43660826/article/details/107492750