hdu 2608 0 or 1(简单数论的题目)

来源:互联网 发布:three.js 360 编辑:程序博客网 时间:2024/04/29 06:15

1、http://acm.hdu.edu.cn/showproblem.php?pid=2608

2、题目大意:

给定一个n,定义S(n) = T(1) + T(2) + T(3)…..+T(n),T(n)是n的所有因子的和,最后输出S(n)%2的值

3、解题思路(参考网上)

比赛的时候出了这么一道题目,当时拿到题目首先想到的是一个一个的计算,但是发现n的值太大了,这样做无疑会导致超时,然后又想这样的题目是不是有什么人规律,先是打表打s(n)打了100个,没发现规律,打T(n)打了200个也没找到规律,当时看着好多人都过了,觉得题目好像很简单,就一直纠结的找规律,到比赛结束也没发现,简直是弱爆了,今天补题,又是打表又是推式子都没用,后来还是看的网上的代码,看到了这样一个规律“能完全开方或者是能整除2后再能完全开方的数%2都是1,”,拿到这样的规律后,我去我打的表中验证发现确实是有这样的规律

首先看前50个T(n),%2=1的有1,2,4,8,9,16,18,25,32,36,49,50;

这里边有1 ,4,9,16,25,36,49,这些正好是1,2,3,4,5,6,7的平方

再看2,8,18,32,50,这些刚好满足整除2后能够完全开方

看了网上解题思路后2分钟就AC了,不过真心想知道拿到这么一个题目怎么下手找规律

4、题目:

0 or 1

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


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
3123
 

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

Author
yifenfei
 

Source
奋斗的年代
 

Recommend
yifenfei   |   We have carefully selected several similar problems for you:  2604 2609 2617 2612 2610
5、AC代码:
#include<stdio.h>#include<algorithm>using namespace std;int main(){    int t,n,sum;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        sum=0;        for(int i=1;i<=n;i++)        {            if(i*i<=n)            sum++;            if(i*i*2<=n)            sum++;            if(i*i>n)            break;        }        printf("%d\n",sum%2);    }    return 0;}

打表代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){    int t,n,s,sum;    for(int k=1; k<=100; k++)    {        sum=0;        for(int i=1; i<=k; i++)        {            s=0;            for(int j=1; j<=i; j++)            {                if(i%j==0)                {                    s+=j;                }            }            printf("t(%d)=%d ",i,s%2);            sum+=s;        }        printf("s(%d)=%d\n",k,sum%2);    }    return 0;}



0 0
原创粉丝点击