UVA 11395——Sigma Function

题目描述:

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 σ.

输入一个n然后输出n以内的因子和为偶数的数的个数

样例:
input:

4

3

10

100

1000

output:

Case 1: 1

Case 2: 5

Case 3: 83

Case 4: 947

题意非常容易理解,但是这道题n的数据范围是10的12次方,单单只是循环一遍就会TLE,更别谈找因子求和了,所以这个题再度用到之前提到的算数基本定理的应用

从第二个式子出发的话就可以发现只要式子中的任意一项是偶数最后的结果就必然是偶数,我们观察这个式子的特点,每次求和都会有一个1,那么如果后面pi的n次幂只和为偶数的话这一项的结果结果就会为奇数,而对于质因子而言,质数中只有2是偶数,2的任意n次幂之和肯定是偶数,+1之后就会变成奇数,所以如果这个数的质因子都是2,也就是说这个数是2的n次方的话,他的因子和就肯定是奇数。再看其他情况,现在我们可以确定因子2的那一项的结果肯定是奇数,那么只能使其他项的结果为偶数,结果才可能为偶数,对于一个奇数,任意n次方肯定都为奇数,那么只要项数是奇数,那我算出来的结果就肯定是奇数,+1之后变成偶数就可以满足题目要求,我们来求反面的情况,如果要使我最后的结果为奇数,那么我每一项的结果算出来应该都是奇数,也就是说我的每一个质因子都出现了偶数次,这种情况很明显就是完全平方数,所以最后的答案只需要从n个数中减去完全平方数的个数和2的n次方的数的个数就行,但是这两种数中又存在重叠的部分,不难算出其实我最后需要减去的是x^2和2*x^2类型的数,那么怎么算这两种数的个数,其实直接开方就行了,sqrt算出的是小于等于该数的最大的x^2的x其实就代表了n以内有x个完全平方数。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXM=1000010;



int main()
{
    int T;
    scanf("%d",&T);
    int cas=1;
    while(T--)
    {
        long long n;
        scanf("%lld",&n);
        long long num1=sqrt(n);
        long long num2=sqrt(n/2);
        printf("Case %d: %lld\n",cas,n-num1-num2);
        cas++;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值