poj 3495 Bitwise XOR of Arithmetic Progression

Bitwise XOR of Arithmetic Progression
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 777 Accepted: 178

Description

Write a program that, given three positive integers xy and z (xyz < 232x ≤ y), computes the bitwise exclusive disjunction (XOR) of the arithmetic progression xx + zx + 2z, …, x + kz, where k is the largest integer such that x +kz ≤ y.

Input

The input contains multiple test cases. Each test case consists of three integers xyz separated by single spaces on a separate line. There are neither leading or trailing blanks nor empty lines. The input ends once EOF is met.

Output

For each test case, output the value of  on a separate line. There should be neither leading or trailing spaces nor empty lines.

Sample Input

2 173 11

Sample Output

48

Source



【分析】

http://www.cnblogs.com/SilverNebula/p/6798249.html



【代码】

//poj 3495 Bitwise XOR of Arithmetic Progression
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(ll i=j;i<=k;i++)
using namespace std;
ll x,y,z;
inline ll solve(ll a,ll b,ll c,ll n)
{
	if(!n) return 0;
	ll ans=0;
	ans+=b/c*n;
	ans+=a/c*n*(n-1)/2;
	return ans+solve(c,(a*n+b)%c,a%c,((a%c)*n+b%c)/c);
}
int main()
{
	while(scanf("%lld%lld%lld",&x,&y,&z)!=EOF)
	{
		ll ans=0;
		fo(i,0,31)
		  ans|=((solve(z,x,(ll)1<<i,(y-x)/z+1)&(ll)1)<<i);
		printf("%lld\n",ans);
	}
	return 0;
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值