您的位置:首页 > 其它

LightOJ - 1336 Sigma Function (n以内约数和为奇数(偶数)的数的个数)

2017-07-06 09:20 323 查看
Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma (σ). This function actually denotes the sum of all divisors of a number. For example σ(24) = 1+2+3+4+6+8+12+24=60. Sigma of small numbers is easy to find
but for large numbers it is very difficult to find in a straight forward way. But mathematicians have discovered a formula to find sigma. If the prime power decomposition of an integer is



Then we can write,



For some n the value of σ(n) is odd and for others it is even. Given a value
n, you will have to find how many integers from 1 to
n have even value of σ.

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1012).

Output
For each case, print the case number and the result.

Sample Input
4

3

10

100

1000

Sample Output
Case 1: 1

Case 2: 5

Case 3: 83

Case 4: 947

这里n很大不能直接暴力的

关于sigma(n)的式子有个想法是这样的:sigma(n)=(1+q1+...+q1^e1)*(1+q2+...+q2^e2)*....*(1+qn+...+qn^en)

很显然当q[i]不为2的时候,当e[i]为奇数时括号内的和为偶数,e[i]为偶数时括号内的和为奇数,

当q[i]==2时,括号内的和必为奇数;

这个题到这一步我都想到了,但接下来的我就GG了....

这个题直接求偶数的个数的话就是直接求存在q[i]不为2且e[i]为奇数的数的个数,但这个我想了很久觉得好像没什么办法可以求出来(可能是因为我很菜吧)

那么可以反着来:求sigma为奇数的个数,然后减一减

当sigma为奇数的时候,必有:q[i]不为2的项的e[i]全部为偶数

于是sigma(s)为奇数的时候,s可以以两种方法得到:s=a^2,s=2*b^2;

(不管a原来的sigma是怎么样的,a^2一定是“所有的e[i]都为偶数,使得任意括号内的和都为奇数,从而a^2为奇数”)

(b也与a一样,但是a的2的次数一定也是偶数,然而sigma为奇数的数中,2的次数是可奇可偶的,所以有乘以2)

然后s还要满足s<=n,

于是a的个数,b的个数分别为sqrt(n),sqrt(n/2),所以sigma为奇数的数的个数为sqrt(n)+sqrt(n/2)....然后减一减

(呜哇~做过一遍的题还不会做,感觉好伤)

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=2e5+3;
LL n;

int main()
{
int T;
scanf("%d",&T);
int cas=0;
while(T--)
{
scanf("%lld",&n);
printf("Case %d: %lld\n",++cas,n-(LL)sqrt(n)-(LL)sqrt(n/2));
}
return 0;
}
大概就是这样,如有错误,还望指正

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