NC243977. Factor Difference
描述
输入描述
The first line contains an integer (), representing the number of test cases.
The following lines, each line contains an integer ().
输出描述
For each test case, output an integer representing the answer.
示例1
输入:
3 1 2 3
输出:
24 105 935
C++(g++ 7.5.0) 解法, 执行用时: 20ms, 内存消耗: 528K, 提交时间: 2022-10-07 20:47:21
#include<bits/stdc++.h> using namespace std; typedef long long ll; int t,n; bool is_pre(ll x){ if(x<2) return false; if(x==2) return true; for(int i=2;i*i<=x;i++) if(x%i==0) return false; return true; } int main(){ cin>>t; while(t--){ cin>>n; ll p1=1+n; while(is_pre(p1)==false) p1++; ll p2=p1+n; while(is_pre(p2)==false) p2++; ll p3=p2+n; while(is_pre(p3)==false) p3++; if(p1*p1>p3) cout<<p1*p2*p3<<endl; else cout<<p1*p1*p1*p2<<endl; } }
C++(clang++ 11.0.1) 解法, 执行用时: 8ms, 内存消耗: 464K, 提交时间: 2023-05-24 14:05:21
#include<iostream> using namespace std; bool isprime(int n) { for(int i=3;i*i<=n;i+=2){ if(n%i==0)return 0; } return 1; } int main(){ int t; cin>>t; while(t--){ int n; cin>>n; if(n==1){ cout<<24<<endl; continue; } int f=0; long long ans=1; int i=n+1; while(f<3){ if(i%2==0){ i+=1; } else if(isprime(i)){ ans*=i; i+=n; f++; }else i+=2; } cout<<ans<<endl; } }
Python3 解法, 执行用时: 189ms, 内存消耗: 4624K, 提交时间: 2022-10-07 10:53:35
def prime(n): for i in range(2,int(n**0.5)+1): if n % i == 0: return False return True for _ in range(int(input())): n = int(input()) if n == 1: print(24) continue x = 0 ans = 1 low = 1 + n while x < 3: if prime(low): ans *= low low += n x += 1 else: low += 1 print(ans)