LightOJ-1336-Sigma Function (算术基本定理)

版权声明:欢迎评论与转载,转载时请注明出处! https://blog.csdn.net/wjl_zyl_1314/article/details/84706194

原题链接:
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,找出1~n之间的所有的σ(x)为偶数的个数。
题解:
根据偶*偶=偶,偶*奇=偶,奇*奇=奇,肯定是算奇数简单,所以算出所有的奇数,最终n-奇数个数就是偶数的个数。根据σ函数得出,必需要函数每一项都要是奇数。
如果px==2,则px^(ex+1)-1必定为奇数;
如果px!=2,则px一定为奇数,由于函数是由等比数列求和得到的(1,px^1,px^2,…px^ex),由于奇数*奇数=奇数,所以此项共有ex+1个奇数相加,所以当ex为偶数时,此项为奇数。
以上就是两种为奇数的可能性:1.px为2 或者2.ex为偶数
由于ex为偶数,所以一定可以化简为某个数的平方,所以只要找出n以内的平方数就能够找出所有的满足第二个条件的个数,再加上第一个函数,有一个质因子是2,所以在平方数乘以二倍即可即2x^2.
为什么只用乘以一次2,而不是2的i此方呢?
因为如果i为偶数,那么可以换成某个数的平方,与第一个重复,如果是奇数都可以化成2
x^2的格式。
综上所述:
只要找出所有的x^2和,2*x^2的个数就找出了所有的奇数可能。
附上AC代码:

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        LL n,a,b;
        scanf("%lld",&n);
        a=sqrt(n);
        b=sqrt(n/2);
        printf("Case %d: %lld\n",cas,n-a-b);
    }
    return 0;
}

欢迎评论!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/84706194