Codeforces Round #461 (Div. 2) B. Magic Forest(异或的性质)

B. Magic Forest
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Imp is in a magic forest, where xorangles grow (wut?)

A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order n to get out of the forest.

Formally, for a given integer n you have to find the number of such triples (a, b, c), that:

  • 1 ≤ a ≤ b ≤ c ≤ n;
  • , where  denotes the bitwise xor of integers x and y.
  • (a, b, c) form a non-degenerate (with strictly positive area) triangle.
Input

The only line contains a single integer n (1 ≤ n ≤ 2500).

Output

Print the number of xorangles of order n.

Examples
input
6
output
1
input
10
output
2
Note

The only xorangle in the first sample is (3, 5, 6).

 题意:

给你一个数字n,让你找到一个三角形,满足

  • 1 ≤ a ≤ b ≤ c ≤ n;
  • a^b^c=0;    
  • 让你找到这样的三角形有多少个 。昨晚刚做的时候,对异或运算的理解有问题......以为是真假逻辑运算.....今天看了别人写的分析才明白过来,异或运算是对两个数的二进制进行的运算,相同为0,不同为1,这样得到了一个新的数字。再来分析条件 a^b^c=0,所以我们把a^b看作一个整体的话,就很容易想到a^b=c,这样我们进行0(n^2)的遍历就可以了,注意一个细节,那就是a<b<c;所以每次遍历的起始点要处理下,ac代码如下:
  • #include<stdio.h>
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            int i,j,sum=0,c;
            for(i=1;i<n;i++)
            {
                for(j=i+1;j<=n;j++)
                {
                    c=i^j;
                    if(c>=1&&c<=n&&c>j&&i+j>c&&i+c>j&&j+c>i)
                        sum++;
                }
            }
            printf("%d\n",sum);
        }
    
    
    
        return 0;
    }

     

 

 

posted @ 2018-02-08 17:06  zero1998  阅读(332)  评论(0编辑  收藏  举报