NC209465. BasicGcdProblem
描述
输入描述
The input contains multiple test cases. The first line of input contains one integer .In the following lines, each line contains two integers () describing one question.
输出描述
For each test case, output one integer indicating the answer.
示例1
输入:
2 3 3 10 5
输出:
3 25
C(clang 3.9) 解法, 执行用时: 1076ms, 内存消耗: 9596K, 提交时间: 2020-07-20 12:29:08
#include<stdio.h> #include<math.h> #define M (int)(1e9+7) int gcd(int n); int f(int n,int c) { if(n==1) return 1; return 1ll*c*f(gcd(n),c)%M; } int gcd(int n) { int i,head; head=sqrt(n); for(i=2;i<=head;i++) if(n%i==0) return n/i; return 1; } int main() { int t,n,c,i; scanf("%d",&t); for(i=0;i<t;i++) { scanf("%d %d",&n,&c); printf("%d\n",f(n,c)); } }
C++14(g++5.4) 解法, 执行用时: 1165ms, 内存消耗: 9700K, 提交时间: 2020-07-25 20:09:30
#include<bits/stdc++.h> typedef long long ll; using namespace std; const int M=1e9+7; int main(){ int t; scanf("%d",&t); while(t--){ int n,c; ll ans=1; scanf("%d %d",&n,&c); for(int i=2;i*i<=n;i++){ while(n%i==0){ n/=i; ans=ans*c%M; } } if(n!=1) ans=ans*c%M; printf("%lld\n",ans); } return 0; }
C++(clang++ 11.0.1) 解法, 执行用时: 1336ms, 内存消耗: 9696K, 提交时间: 2023-05-02 17:48:26
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1e9+7; int T; int n,c; int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&c); ll ans=1; for(int i=2;i*i<=n;++i) while(n%i==0){ n/=i; ans=ans*c%mod; } if(n>1)ans=ans*c%mod; printf("%lld\n",ans); } }