【代码超详解】LightOJ 1336 Sigma Function(约数和定理 +思维,0 ms)

一、题目描述

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

二、算法分析说明

在这里插入图片描述
已知:在这里插入图片描述在这里插入图片描述奇数 × 奇数 = 奇数,偶数 × 奇数 = 偶数,偶数 × 偶数 = 偶数。
要求 [1, n] 内 S 是偶数的数 X 的个数,但 S 是偶数的条件不好确定。不过,由于任意个奇数相乘仍然是奇数,所以可以利用 “正难则反” 思想,先筛选出约数和 S 为奇数的数的个数,再用总数减去这个数。
当一个数 X 含有质因子 2 时,2 的正整数次方都是偶数,这些偶数相加也是偶数,再加 1 就是奇数。
当一个数含有其它质因子(都是奇数)时,奇数的正整数次方都是奇数。偶数个奇数相加是偶数,奇数个奇数相加是奇数。所以仅这个数含有该奇质数的偶数次方时,约数和的表达式中对应的乘数才是奇数。
如果一个数的唯一分解式中仅含有奇质数的偶数次方的因子,那么这个数是平方数。在此条件下,如果在该数的唯一分解式中添加 2 的任意次方作为因子,那么它的约数和依然是奇数。归结起来,所有平方数和它们的两倍就是 [1, n] 范围内全部约数和为奇数的数。显然,集合 {n2} 和 {2n2} 是不交的。
所以最终答案是:n - sqrt(n) - sqrt(n / 2)。

三、AC 代码(0 ms)

#include<cstdio>
#include<cmath>
#pragma warning(disable:4996)
unsigned t; unsigned long long n;
int main() {
	scanf("%u", &t);
	for (unsigned i = 1; i <= t; ++i) {
		scanf("%llu", &n);
		printf("Case %u: %llu\n", i, n - (unsigned long long)sqrt(n) - (unsigned long long)sqrt(n / 2));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值