Basic Gcd Problem

Basic Gcd Problem

时间限制: 2 Sec 内存限制: 256 MB

题目描述

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

Z Y B ZYB ZYB constructs a function f c ( x ) f_c(x) fc(x) such that:
f c ( x ) = max ⁡ i = 1... x − 1 c ⋅ f c ( g c d ( i , x ) ) x > 1 f c ( x ) = 1 x > 1 f_c(x)=\max_{i=1...x-1} c \cdot f_c(gcd(i,x)) \quad x>1\\ f_c(x)=1 \quad\quad\quad\quad\quad\quad\quad\quad\quad\quad x>1 fc(x)=i=1...x1maxcfc(gcd(i,x))x>1fc(x)=1x>1
Give some positive integer pairs ( n i , c i ) (n_i, c_i) (ni,ci), ZYB wants to know f c i ( n i ) f_{c_i}(n_i) fci(ni) mod ( 1 0 9 + 7 10^9 +7 109+7).

输入

The input contains multiple test cases. The first line of input contains one integer T ( 1 ≤ T ≤ 1 0 6 ) T(1≤T≤10^6) T(1T106).
In the following T T T lines, each line contains two integers n i , c i ( 1 ≤ n i , c i ≤ 1 0 6 ) n_i, c_i(1≤n_i,c_i≤10^6) ni,ci(1ni,ci106) describing one question.

输出

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

样例输入

2
3 3
10 5

样例输出

3
25

思路

观察这个函数可得
n = p 1 a 1 p 2 a 2 p 3 a 3 . . . p k a k m = ∑ i = 1 k a i a n s = c m n=p_1^{a_1}p_2^{a_2}p_3^{a_3}...p_k^{a_k}\\ m=\sum_{i=1}^{k} a_{i}\\ ans=c^{m} n=p1a1p2a2p3a3...pkakm=i=1kaians=cm

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6+10;
int prime[maxn + 10] , vis[maxn + 10],tot;
inline void prework() {
    for (int i = 2; i <= maxn; i++) {
        if (vis[i] == 0) {
            vis[i] = i;
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && 1ll * prime[j] * i <= maxn; j++) {
            vis[i * prime[j]] = prime[j];
            if (i % prime[j] == 0) break;
        }
    }
}
int qpow(int a,int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = 1ll * res * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return res;
}
int n,c;
inline void work() {
    int k = 0;
    scanf("%d%d", &n, &c);
    while (n > 1) {
        k++;
        n /= vis[n];
    }
    printf("%d\n", qpow(c, k));
}
int main() {
    prework();
    int T = 1;
    scanf("%d", &T);
    while (T--) {
        work();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值