一、题目描述
Find the result of the following code:
long long pairsFormLCM( int n ) {
long long res = 0;
for( int i = 1; i <= n; i++ )
for( int j = i; j <= n; j++ )
if( lcm(i, j) == n ) res++; // lcm means least common multiple
return res;
}
A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).
Output
For each case, print the case number and the value returned by the function ‘pairsFormLCM(n)’.
Sample Input
15
2
3
4
6
8
10
12
15
18
20
21
24
25
27
29
Sample Output
Case 1: 2
Case 2: 2
Case 3: 3
Case 4: 5
Case 5: 4
Case 6: 5
Case 7: 8
Case 8: 5
Case 9: 8
Case 10: 8
Case 11: 5
Case 12: 11
Case 13: 3
Case 14: 4
Case 15: 2
二、算法分析说明

三、AC 代码
这里采用 https://blog.youkuaiyun.com/qq_40924940/article/details/86533392 的 AC 代码,因为我本人写的代码莫名其妙超时。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7+5;
#define ll long long int
bool is_prime[maxn];
ll prime[maxn/10],tot;
void judge()
{
memset(is_prime,true,sizeof is_prime);
tot = 0;
is_prime[0] = is_prime[1] = false;
for(ll i=2;i<maxn;i++)
{
if(is_prime[i])
{
prime[tot++] = i;
for(ll j=i+i;j<maxn;j+=i)
{
is_prime[j] = false;
}
}
}
}
ll slove(ll n)
{
ll res = 1;
for(int i=0;i<tot;i++)
{
if(n % prime[i] != 0)continue;
if(prime[i] * prime[i] > n)continue;
ll k = 0;
while(n % prime[i] == 0)
{
n /= prime[i];
k ++;
}
res *= (k*2+1);
}
if(n > 1)
res *= 3;
return res;
}
int main()
{
int t,cas = 1;
scanf("%d",&t);
judge();
while(t--)
{
ll n;
scanf("%lld",&n);
ll num = slove(n);
printf("Case %d: %lld\n",cas++,(num+1)/2);
}
return 0;
}
本人的代码(TLE,原因不明):
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<bitset>
#pragma warning(disable:4996)
using namespace std;
unsigned long long prime[664579], _PrimeTy, MaxPrime, * prime_end = prime; bitset<10000001> notprime;
inline void gen_prime() {
decltype(_PrimeTy) n = notprime.size() - 1, m = n / 2, L; notprime[0] = notprime[1] = true;
for (decltype(_PrimeTy) i = 2; i <= m; ++i) {
if (!notprime[i]) { *prime_end = i, ++prime_end; }
L = n / i;
for (auto j = prime; j != prime_end && *j <= L; ++j) {
notprime[i * *j] = true; if (i % *j == 0)break;
}
}
for (decltype(_PrimeTy) i = m + 1; i <= n; ++i)
if (!notprime[i]) { *prime_end = i, ++prime_end; }
MaxPrime = *(prime_end - 1);
}
template<class _Ty, class _Rty = unsigned> inline _Rty f(_Ty X) {//打表的最大质数的平方不能超过X的类型_Ty能够存储的最大值,否则计算出的分解上限L会溢出,导致分解成功与否判断出错。
static _Ty L = MaxPrime * MaxPrime; _Rty m, n = 1; _Ty t = min((_Ty)sqrt(X), MaxPrime);
for (auto d = prime; *d <= t; ++d) {
m = 0;
while (X % *d == 0) { X /= *d, ++m; }
n *= 2 * m + 1, t = min((_Ty)sqrt(X), MaxPrime);
if (X == 1)return n;
}
if (X < L)return 3 * n;
return -1;
}
unsigned t, c; unsigned long long n, a, b;
int main() {
gen_prime();
scanf("%u", &t);
for (unsigned h = 1; h <= t; ++h) {
scanf("%llu", &n);
printf("Case %u: %u\n", h, (f(n) + 1) / 2);
}
return 0;
}
博客详细解析了LightOJ 1236题的要求,指出直接实现可能会超时,并提示需要分析代码以找到有效算法。内容包括题目描述、输入输出格式、样例以及通过测试的代码,同时提到作者自己的代码遇到超时问题。
1102

被折叠的 条评论
为什么被折叠?



