牛客多校(2020第四场)B Basic Gcd Problem(质因数分解)

题目链接:https://ac.nowcoder.com/acm/contest/5669/B

题意:

牛客多校(2020第四场)B Basic Gcd Problem(质因数分解)_第1张图片

那本题只要求出n的质因子个数即可

 

 

 1 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 using namespace std;
 8 
 9 #define ll long long
10 const int N = 1e6 + 5;
11 const ll MOD = 1e9 +7;
12 
13 int c,n;
14 
15 int main() {
16     ios::sync_with_stdio(false);
17     cin.tie(0);
18 
19     int t;
20     cin>>t;
21     
22     while(t--) {
23         scanf("%d %d",&n,&c);
24         ll cnt = 1;
25         
26         for (int i = 2; i * i <= n; i++) {
27             while (n % i == 0) {
28                 cnt = cnt * c % MOD;
29                 n /= i;
30             }
31         }
32         if(n!=1) cnt = cnt * c % MOD;
33         cout << cnt << "\n";
34     } 
35     return 0;
36 }

 解法2:先将1~N的质因子个数存储起来再使用

 1 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 using namespace std;
 8 const int N = 1e6 + 5;
 9 const int MOD = 1e9 +7;
10 
11 long long c,n;
12 long long v[N], prime[N], temps[N];
13 
14 void primes(int n)  {
15     memset(v, 0, sizeof(v));
16     memset(prime, 0, sizeof(prime));
17     fill(temps, temps+N, 1); //存储质因数个数
18 
19     int m = 0; //质数数量
20     for (int i = 2; i <= n; i++) {
21         if (v[i] == 0)  {v[i] = i; prime[++m] = i;}
22 
23         for (int j = 1; j <= m; j++) {
24             if(prime[j] > v[i] || prime[j] > n / i) break; //要赋给i * prime[j]的最小质因子为prime[j],如果prime[j]比i的最小质因子都大,那肯定得退出循环了
25             v[i*prime[j]] = prime[j];
26             temps[i*prime[j]] = temps[prime[j]] + temps[i]; //递推该质因数的个数
27         }
28     }
29 }
30 
31 long long fun(long long x,long long n) {
32     long long res = 1;
33     while (n > 0) {
34         if (n & 1)  res = res * x % MOD;
35         x = x * x % MOD;
36         n >>= 1;
37     }
38     return res;
39 }
40 
41 int main() {
42     int t;
43     cin>>t;
44     primes(N);    
45     while(t--) {
46         scanf("%lld %lld",&n,&c);
47         if (n == 1)
48             cout << "1\n";
49         else
50             cout<"\n";
51     } 
52     return 0;
53 }

 

你可能感兴趣的:(牛客多校(2020第四场)B Basic Gcd Problem(质因数分解))