CodeForces 922B 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
Copy
6
output
Copy
1
input
Copy
10
output
Copy
2
Note

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

题意是在1-n中找到三个数满足 a<b<c能构成三角形,且a^b^c==0

根据异或的特点可以知道 如果A^B==0 则A==B

所以枚举a和b,c就是a^b,验证c是否满足条件即可。

[cpp]  view plain  copy
  1. #include <bits/stdc++.h>  
  2. #include <cstring>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     long long n;  
  8.     cin>>n;  
  9.     long long ans=0;  
  10.     for(int i=1;i<=n-2;i++)  
  11.     {  
  12.         for(int j=i+1;j<=n-1;j++)  
  13.         {  
  14.             int k=(i^j);  
  15.        //     cout<<i<<' '<<j<<' '<<k<<endl;  
  16.             if(k>j&&k<=n&&i+j>k)  
  17.                 ans++;  
  18.         }  
  19.     }  
  20.     cout<<ans<<endl;  
  21.     return 0;  
  22. }  

猜你喜欢

转载自blog.csdn.net/qq_39027601/article/details/80075825