您的位置:首页 > 其它

NYOJ 865 解题报告

2015-07-26 11:12 239 查看

F(x)

时间限制:1000 ms  |  内存限制:65535 KB

[align=center]难度:3[/align]

描述 我们定义 F(x)是满足 x  mod(a*b) == 0这样的a,b的组数。现在给你一个n,你需要求出 F(n)

输入有多组测试数据。

每组测试数据输入一个整数n (1 <= n <= 10^11)输出每组测试数据输出 Case x: y ,x 表示第x组测试数据,y表示F(n)的值,细节参考样例。样例输入
1
2
3
4

样例输出
Case 1: 1
Case 2: 3
Case 3: 3
Case 4: 6

       这道题又是考察乘性函数的题目。首先我们分析一下 x  mod(a*b) == 0这个式子,可以发现a*b其实就是x的因子。记d为x的一个因子,那么所有d的因子拆分d=a*b的种数就是F(n)的值。写成数学式子,就是

,其中

是因子个数函数。数论里知道,因子个数函数是一个乘性函数,所以F也是一个乘性函数。所以,我们能推出

,其中,



到此,这道题目就解决了!不过要注意当n=1时不能用上述公式计算,而是直接输出答案为1就好。

       附上我的代码:

#include <stdio.h&
9ecf
gt;

int main()
{
long long n;
int case_times=1;
while((scanf("%lld",&n))!=EOF)
{
long long sum=1;
if(n>1)
{
long long i,temp=n,times;
int flag=0;
for(i=2;i*i<=temp;i++)
{
flag=times=0;
while(n%i==0)
{
flag=1;
times++;
n/=i;
}
if(flag==1)
sum=sum*(1+times)*(2+times)/2;
}
}
if(n>1)
sum=sum*3;
printf("Case %d: %lld\n",case_times++,sum);
}
return 0;
}

        再来看看标程的:

#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>
#include <ctime>
using namespace std;

const int N = 1e2+10;
const int inf = 1<<30;
typedef long long LL;
LL find(LL k)
{
LL i, j, sum = 0;
for(i = 1;i * i <= k;i++)
{
if(k % i ==0)
{
sum++;
if(i*i != k)
sum++;
}
}
return sum;
}
LL solve(LL n)
{
LL sum = 0;
for(LL i = 1 ;i * i<= n;i++)
{
if(n % i == 0)
{
sum += find(i);
LL j = n/i;
if(j != i)
sum += find(j);
}
}
return sum;
}
int main()
{
//freopen("Input.txt", "r", stdin);
//freopen("Output.txt", "w", stdout);
LL i, j, n, T = 1;
while(~scanf("%lld", &n))
{
LL sum = solve(n);
printf("Case %lld: %lld\n", T++,sum);
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: