现在的位置: 首页 > 综合 > 正文

HDU-2608 0 or 1 数论题

2012年08月04日 ⁄ 综合 ⁄ 共 1940字 ⁄ 字号 评论关闭

0 or 1

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 966    Accepted Submission(s): 199

Problem Description

 

Solving
problem is a interesting thing. Yifenfei like to slove different
problem,because he think it is a way let him more intelligent. But as we
know,yifenfei is weak in math. When he come up against a difficult math
problem, he always try to get a hand. Now the problem is coming! Let we
define T(n) as the sum of all numbers which are positive integers can divied n. and S(n) = T(1) + T(2) + T(3)…..+T(n).
 

 

Input

 

The
first line of the input contains an integer T which means the number of
test cases. Then T lines follow, each line consists of only one
positive integers n. You may assume the integer will not exceed 2^31.
 

 

Output

 

For each test case, you should output one lines of one integer S(n) %2. So you may see the answer is always 0 or 1 .
 

 

Sample Input
3
1
2
3
 

 

Sample Output
1
0
0

Hint

Hint S(3) = T(1) + T(2) +T(3) = 1 + (1+2) + (1+3) = 8
S(3) % 2 = 0

 
 
  各种打表啊,有木有,以为有规律啊。找了N久的规律,发现了除2以外的质数的T[]一定为0,其他就没什么发现了。还是要多见识,多学习。
  看了别人的解题思路后才明白这题还是建立在严谨的数学逻辑上,现将自己的理解说一下,也便以后复习,回味。
  对于每一个数N,我们可以将其拆分为  N= p1^e1 * p2^e2 * p3^e3 * ...  * pm^em. 其中p1,p2...都是质因子,这可以回想到我们求两数的最大公约数时,我们就是将一个数拆分成质因子相乘的形式。那么首先提出一个问题,那就是数N有多少个因子了,知道排列组合的话,很快就能知道,这个结果是 K=( e1+1 )* ( e2+1 )* ... *( em+1 )因为每个质因子 pi 我们都可以取 0-ei 个来组合成一个因子,当每个都取零时,这个因子就是 1 ,当每个都去最大值时,这个因子就是 N 本身,那么如何得到这 K 个因子的和呢?我们补全 N 的质因子乘积形式,将所有质因子补全,N= 2^e1 * 3^e2 * 5^e3 * 7^e4 * 11^ e5 *......   T[N]= [ ( 2^0+2^1+...+2^e1 )*( 3^1+3^2+...+3^e2 )*...*( pm^0+pm^1+...+pm^em ) ]% 2;   我们知道 ( 2^0+2^1+...+2^e1 )% 2= 1 恒成立(因为有2^0=1),所以T[N]的值只取决与后面的乘式%2 是否出现了0,那么后面的式子怎样才会是零呢?很简单,因为所有的质素都是奇数,所以当除2以外的存在某一个质因子表达式为偶数项的时候就会为零了,也即只要满足至少存在一个 ei 的值为奇数(项数为ei- 0+ 1)。当然这题中,我们不会去直接计算为0的T[]有多少多,这很困难,而是从反面求解,即计算T[]为1的数的个数,这样,求S[N]也就转化为从1-N有多少个T[i]为1。回到前面,如果T[i]为1,那么除 2 之外所有质因子的 ei 都为偶数,这个条件就很好解决了,而不像前面的求解满足至少一项 ei 为奇数,既然除2之外所有的 ei 都为偶数,那么这个数一定会是 i= x^2 或者是 i= 2* x^2的形式。所以统计从 1-N 之间有多少个满足T[i]= 1数吧。
 
  代码如下:
#include <stdio.h> 

// 请用G++提交,C++不支持%lld

int main(  )
{  
    int T;
    scanf( "%d", &T );
    while( T-- )
    {
        long long x, cnt= 0;
        scanf( "%lld", &x );
        for( long long i= 1; i* i<= x; ++i )
        {
            cnt++;
            if( 2* i* i<= x )
            {
                cnt++;
            }
        }
        printf( "%lld\n", cnt% 2 );
    }
    return 0;
}

 

抱歉!评论已关闭.