Codeforces Round #461 (Div. 2) B. Magic Forest

B. Magic Forest

time limit per test 1 second
memory limit per test 256 megabytes

Problem Description

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).


解题心得:

  1. 题意是从1到n中选出三个数(可以相同),这三个数异或和为0,并且三个数可以形成一个三角形,问一共有多少中方案。
  2. n最大是2500,跑三重循环不现实,但是想想还是就明白的,三个数异或和为0,那么两个数异或起来肯定等于第三个数,这样跑双重循环就可以了。然后需要标记找过的三角形,由于异或起来的第三个数并不能确定和前两个数的大小关系,所以只能hash标记,这里就很坑了,之前选了一个233来hash,结果被卡了,想了半天才想到被卡了hash,连2333都会被卡。晕哦,最后23333过了。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e7+10;
typedef long long ll;
map<ll,ll> maps;
int main(){
    ll n;
    ll a[5],ans=0;
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++){
        for(ll j=i+1;j<=n;j++){
            ll c = i^j;//由前两个数得到第三个数
            if(c > n || c < 1)//判读是否出了边界
                continue;
            a[0] = i;a[1] = j;a[2] = c;
            sort(a,a+3);
            ll temp = a[0];
            temp = temp*23333+a[1];
            temp = temp*23333+a[2];
            if(maps[temp] == 233)
                continue;
            maps[temp] = 233;
            if(a[0] + a[1] > a[2])//两小边之和大于第三边
                ans++;
        }
    }
    printf("%lld
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/GoldenFingers/p/9107177.html