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

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

思路:

看到这一题首先想到了用动态规划的思想来存储最大值为i时的组数。

对于计算满足最大值为i时的组数时,我一开始用了三层循环,然而一定会超时。然后就想到了异或的性质,当c=a^b,那么a^b^c==0.于是就可以根据i和j直接计算出c的值,再判断c是否满足条件就可以了。由于会有重复的三元组,我们可以规定c必须小于j。


代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
using namespace std;
int n;
long long dp[2505];
int main()
{
    scanf("%d",&n);
    dp[0]=0;
    for(int i=1;i<=n;i++)
    {
        dp[i]=dp[i-1];
        for(int j=1;j<=i;j++)
        {
            int k=i^j;
            if(k+j>i && k<j) dp[i]++;
        }
    }
    printf("%lld\n",dp[n]);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值