Problem Description
Rikka is a high school girl suffering seriously from Chūnibyō (the age of fourteen would either act like a know-it-all adult, or thinks they have special powers no one else has. You might google it for detailed explanation) who, unfortunately, performs badly at math courses. After scoring so poorly on her maths test, she is faced with the situation that her club would be disband if her scores keeps low.
Believe it or not, in the next exam she faces a hard problem described as follows.
Let’s denote f(x) number of ordered pairs satisfying (a * b)|x (that is, x mod (a * b) = 0) where a and b are positive integers. Given a positive integer n, Rikka is required to solve for f(1) + f(2) + . . . + f(n).
According to story development we know that Rikka scores slightly higher than average, meaning she must have solved this problem. So, how does she manage to do so?
Input
There are several test cases.
For each test case, there is a single line containing only one integer n (1 ≤ n ≤ 1011).
Input is terminated by EOF.
Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.
Sample Input
1
3
6
10
15
21
28
Sample Output
Case 1: 1
Case 2: 7
Case 3: 25
Case 4: 53
Case 5: 95
Case 6: 161
Case 7: 246
https://blog.csdn.net/faithdmc/article/details/38332183
//所求的数即是能够组成a*b*c<=n的对数,是一道排列组合的问题。采用暴力枚举的方式枚举a,b之后根据c计算结果
//这是不考虑时间复杂度的算法
#include
#include
using namespace std;
typedef unsigned long long int LLi;
LLi n;
int main()
{
int the_case = 1;
while (scanf_s("%lld", &n) != EOF)
{
LLi res = 0;
for (LLi a = 1; a*a <= n; a++)
{
for (LLi b = a; b*b*a <= n; b++)
{
LLi c = n / (a*b);
LLi mid = 0LL;
if (a == b)
mid += 1LL;
else
mid += 3LL;
if (c>b)
{
if (a == b)
mid += 3LL * (c - b);
else
mid += 6LL * (c - b);
}
res += mid;
}
}
printf("Case %d: %lld\n", the_case++, res);
}
return 0;
}
// ConsoleApplication40.cpp: 定义控制台应用程序的入口点。
//
/*
那就这么考虑:
对于f[x],xmod(a*b)=0,即存在c,使a*b*c=x,f[x]即为这样的a,b的组合个数。
题目要求的是f[1]+f[2]+f[3]+…+f[n-1]+f[n]的和,很明显数据很大,不适宜直接使用直接枚举的方法。
可以使用排序组合的思路对题目进行简化,以降低时间复杂度。
f[1]+f[2]+f[3]+…+f[n-1]+f[n]的和即可这样理解:a*b*c<=n,求这样的a,b,c的组合个数。
以下顺序为将a、b、c按递增顺序排列:
A、a=b=c,
B、a=b<c
C、a<b=c
D、a<b<c
由于题目abc可以任意排列,故最终答案为A+(B+C)*3+D*6,剩下的任务就是求ABCD了。
*/
//#include "stdafx.h"
#include
#include
#include
using namespace std;
int main()
{
__int64 n, ans, i, j, a, b, tag = 1;
while(~scanf_s("%I64d", &n))//多组数据输入
{
ans = 0;
a = pow((double)n, 1.0 / 3.0);
while(a*a*a a++;
if(a*a*a>n)
a--;
ans += a;
for (i = 1; i <= a; i++)///枚举a的可能取值
{
__int64 tmp = n / i;
b = sqrt(tmp);
while (b*b b++;
if (b*b>tmp)
b--;
ans += (b - i + tmp / i - i) * 3;
for (j = i + 1; j <= b; j++)
ans += (tmp / j - j) * 6;
}
printf("Case %I64d: %I64d\n", tag++, ans);
}
return 0;
}
这样应该可以了,这个程序我在VS2017中运行没有问题,假若头文件没有显示,麻烦您自己写下,还有您能说一下之前那个程序您运行的错误显示结果是哪种呢?
是WA,TLE,Runtime Error,还是Compile Error?