对于计算满足最大值为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;
}