CodeForces - 922B (异或)

本文介绍了一道算法题目,任务是找出所有可能的整数三角形边长组合,使得这三个边长通过异或运算得到0。文章提供了完整的C++代码实现,并详细解释了解题思路。

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 exceedingn, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of ordern 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 thebitwise xor of integersx 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.

Example
Input
6
Output
1
Input
10
Output
2
Note

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

题意:给你一个数n,要求你找到这样的一个三角形三边分别为a,b,c,且a^b^c=0(异或),问满足条件的三角形有几个?

解题的关键是那个异或式,我们知道c^c=0,所以c=a^b。有了这个条件我们就可以直接遍历枚举找答案了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3fffffff
using namespace std;
typedef long long LL;
const int N=1e5+1;
int main()
{
    int n,ans,tmp;
    while(~scanf("%d",&n))
    {
        ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                tmp=i^j;
                if(tmp>=1&&tmp<=n&&tmp>=j&&i+j>tmp)//注意tmp是有范围的,且要满足三角形两边之和大于第三边的条件
                    ans++;
            }
        }
        printf("%d\n",ans);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值