解题报告:UVA11395 Sigma Function 找规律

来源:互联网 发布:东方网络复牌 编辑:程序博客网 时间:2024/05/13 06:41
Sigma Function
Time Limit:2000MS    Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

Description

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 valuen, you will have to find how many integers from 1 ton 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中经过公式运算得到的值有多少个是偶数,注意 1 的 公式值 不是 偶数 。


思路:

这题我先是胡乱想了一通最后打到100内表突然看见了83......真是熟悉的数字,然后又突然得出了公式了。。
ans = n-(long long)sqrt(n)-(long long)sqrt(n/2) 

之前胡乱想的想法还是有点帮助的,但是好像没什么实质的作用。就放在最后记录一下自己看吧。。这题A的真是运气



代码:

#include<stdio.h>#include<math.h>int main(){    int T,t=0;scanf("%d",&T);    while(T--){        long long n ;        scanf("%lld",&n);        printf("Case %d: %lld\n",++t,n-(long long)sqrt(n)-(long long)sqrt(n/2));    }return 0;}







我先看题目给定的公式,很明显每个乘号隔开的式子就是一个等比数列求和公式。

于是先将唯一分解式转换成一个更形象的式子


简要的用话语描述第二个式子就是:   将公式的每一项变成 长度为 (ei+1),首项为1,公比为 pi  等比数列的和  。 


那么接下来我们要求的是 1~n 的区间内 公式值为 偶数的 数的个数。

我们知道:

1. 偶数乘以任意数都是偶数,奇数乘以奇数总是奇数。

可知:只要公式中任意一项为偶数,则公式的值一定为偶数。

2. 偶数加上偶数的结果为偶数,偶数加上奇数的结果为奇数。   

可知:那么我们知道当素因子p为偶数时,它的等比数列的和一定为奇数(因为只有第一项为奇数1,其余项都是偶数)。

3.奇数个奇数相加的结果为奇数,偶数个奇数相加的结果为偶数。

可知:当素因子p为奇数时,若对应的e为奇数,那么该等比数列有偶数项,数列和为偶数。反之,对应的e为偶数时,该等比数列有奇数项,对应的等比数列的和为奇数。


综上所得,可知要求的是 1~n中含有奇数个奇数素因子(其实就是除了2以为的其余素因子)的数的个数。



0 0
原创粉丝点击