Codeforces Round#461(Div. 2)(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
6
output
1
input
10
output
2
Note

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



题意:给出数n,找出所有满足下列条件的三角形的个数:

三条边a,b,c,满足1 ≤ a ≤ b ≤ c ≤ n且


解题思路:

异或:

异或,英文为exclusive OR,缩写成xor
异或( xor)是一个数学运算符。它应用于逻辑运算。异或的数学符号为“⊕”,计算机符号为“xor”。其运算法则为:
a⊕b = (¬a ∧ b) ∨ (a ∧¬b)
如果a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0。


枚举b,c两条边,要想符合条件那么a==b⊕c,此时判断a是否小于b和c并且这三边能否构成三角形。


AC代码:


#include<stdio.h>

int main()
{
	int n,a;
	while(~scanf("%d",&n))
	{
		int sum=0;
		for(int c=1;c<=n;c++)
		{
			for(int b=1;b<=c;b++)
			{
				int a=b^c;
				if(a>b||a>c) continue;
				if(a+b>c) sum++;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值