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

B. Magic Forest
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
6
output
1
inputCopy
10
output
2
Note
The only xorangle in the first sample is (3, 5, 6).
题意:给定一个n,要求用从(1,n)中选择3条边组成一个三角形,三角形三边异或起来结果要为0,问能找到多少个这样的三角形。
思路:n<=2500,n^3肯定会炸,暴力去枚举每个三角形是不行的。思考一下,如果已经选出了2边,得到一个xor,那么第3边只有等于xor时才可以使3边异或为0,那么我们只需要枚举三角形的两边,如果可以选择两边异或值为第3边,那么ans++。n^2,AC。
代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=505*505;
int n,ans;
void dfs(int i,int j,int a)
{
    for(;i<=n;i++)
    {
        if(j==0)dfs(i+1,j+1,i);
        if(j==1)
        {
            int b=i^a;
            if(b>a&&b>i&&b<(i+a)&&b<=n)ans++;
        }
    }
}
int main()
{
    scan(n);
    dfs(1,0,0);
    prin(ans);
    return  0;
}

猜你喜欢

转载自blog.csdn.net/swust5120160705/article/details/79381133