POJ3495 Bitwise XOR of Arithmetic Progression
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 772 | Accepted: 175 |
Description
Write a program that, given three positive integers x, y and z (x, y, z < 232, x ≤ y), computes the bitwise exclusive disjunction (XOR) of the arithmetic progression x, x + z, x + 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 x, y, z 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
异或的每一位是独立的,所以可以分别计算每一位的答案。
假设现在正在处理的二进制位为 $ 2 ^ i $ ,我们需要计算
\( \left \lfloor \frac{x}{2^i} \right \rfloor + \left \lfloor \frac{x+z}{2^i} \right \rfloor + \left \lfloor \frac{x+2z}{2^i} \right \rfloor + \left \lfloor \frac{x+3z}{2^i} \right \rfloor + [f(x)] + \left \lfloor \frac{x+(n-1)z}{2^i} \right \rfloor \)
好麻烦啊,换个表示方法:
\( a=z \)
$ b=x $
$ c=2^i $
$ans=\sum_{x=0}^{n-1} \left \lfloor \frac{ax+b}{c} \right \rfloor$
$ans=\sum_{x=0}^{n-1} (\left \lfloor \frac{ax}{c} \right \rfloor +\left \lfloor \frac{b}{c} \right \rfloor +\left \lfloor \frac{(a\%c)*x+b\%c}{c} \right \rfloor) $ (1)
前两项可以提出来用等差数列求和公式算,后一项看着有点麻烦啊
把后一项画出来是这个样子:

发现我们要算的是直线下面的整点的数量,即图中的蓝点数。
为了方便地计算蓝点,重建直角坐标系,像下面那样:

原来的直线方程是
$ \frac{(a\%c) * x + b\%c)}{c} $
现在变成了
$ \frac{cx+(an+b)\%c}{a\%c} $
(斜率取倒数,再算一下x0到n的距离作为截距)
那么
$ ans=\sum_{x=0}^{n-1} \left \lfloor \frac{ax+b}{c} \right \rfloor =\sum_{x=0}^{\lfloor (a\%c)n+(b\%c)/c +1\rfloor} \lfloor \frac{cx+(an+b)\%c}{a\%c} \rfloor $
可以发现这是一个可以递归计算的形式。
所以每次递归处理余下的部分,累加计算(1)式的前两项,算出这一位的值以后,判断二进制的这一位是奇数还是偶数,统计最终答案。
计算会爆int。
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
LL calc(LL a,LL b,LL c,LL n){
if(!n)return ;
LL tmp=(LL)a/c*n*(n-)/;
tmp+=(LL)b/c*n;
return tmp+calc(c,(a*n+b)%c,a%c,((a%c)*n+b%c)/c);
}
int main(){
LL x,y,z;
while(scanf("%lld%lld%lld",&x,&y,&z)!=EOF){
LL ans=;
for(int i=;i>=;i--){
ans|=(calc(z,x,1ll<<i,((LL)y-x++z-)/z)&1ll)<<i;
}
printf("%lld\n",ans);
}
return ;
}
POJ3495 Bitwise XOR of Arithmetic Progression的更多相关文章
- CF 1114 E. Arithmetic Progression
E. Arithmetic Progression 链接 题意: 交互题. 有一个等差序列,现已打乱顺序,最多询问60次来确定首项和公差.每次可以询问是否有严格大于x的数,和查看一个位置的数. 分析: ...
- Dirichlet's Theorem on Arithmetic Progression
poj3006 Dirichlet's Theorem on Arithmetic Progressions 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. ...
- Find Missing Term in Arithmetic Progression 等差数列缺失项
查找等差数列中的缺失项. e.g.Input: arr[] = {2, 4, 8, 10, 12, 14} Output: 6 Input: arr[] = {1, 6, 11, 16, 21, 31 ...
- BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数 ...
- codeforces C. Arithmetic Progression 解题报告
题目链接:http://codeforces.com/problemset/problem/382/C 题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能 ...
- cf C. Arithmetic Progression
http://codeforces.com/contest/382/problem/C 题意:给你n个数,然后让你添加一个数使得n+1个数能形成这样的规律,a[1]-a[0]=a[2]-a[1]=a[ ...
- CF1114E Arithmetic Progression(交互题,二分,随机算法)
既然是在CF上AC的第一道交互题,而且正是这场比赛让我升紫了,所以十分值得纪念. 题目链接:CF原网 题目大意:交互题. 有一个长度为 $n$ 的序列 $a$,保证它从小到大排序后是个等差数列.你不知 ...
- Codeforces 1114E - Arithmetic Progression - [二分+随机数]
题目链接:http://codeforces.com/problemset/problem/1114/E 题意: 交互题,有一个 $n$ 个整数的打乱顺序后的等差数列 $a[1 \sim n]$,保证 ...
- HDU 5143 NPY and arithmetic progression(思维)
http://acm.hdu.edu.cn/showproblem.php?pid=5143 题意: 给定数字1,2,3,4.的个数每个数字能且仅能使用一次,组成多个或一个等差数列(长度大于等于3), ...
随机推荐
- Where to go from here
Did you get through all of that content? Congratulations! You've learnt the fundamentals of algorith ...
- 3dContactPointAnnotationTool开发日志(十九)
增加了输出接触点信息到文件功能.
- Qt应用程序图标
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt应用程序图标 本文地址:http://techieliang.com/2017/1 ...
- js实现倒计时60秒的简单代码
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...
- ifstat查看网络流量的原理
ifstat查看网卡流量的原理:读的是哪个/proc/ 接口啊 同diskIO一样,网络的IO也同样有统计计数的,是/proc/net/dev一个典型的输出就是这个样子的: root@station6 ...
- 【Python】Python之文件操作
1. file=open('xxx.txt', encoding='utf-8'),open()函数是Python内置的用于对文件的读写操作,返回的是文件的流对象(而不是文件本身,所以使用的方法都是流 ...
- Android UI设计的基本元素有哪些
在android app开发如火如荼的今天,如何让自己的App受人欢迎.如何增加app的下载量和使用量....成为很多android应用开发前,必须讨论的问题.而ui设计则是提升客户视觉体验度.提升下 ...
- JS作用域-面向对象
1. 其它语言是以代码块作为作用域的.下面程序会报错(如C,C++中),因为局部变量name只在{ }代码块中生效.打印console.writeline(name)中的name时就会报错. pu ...
- 详细图解jQuery对象,以及如何扩展jQuery插件
详细图解jQuery对象,以及如何扩展jQuery插件 早几年学习前端,大家都非常热衷于研究jQuery源码.我还记得当初从jQuery源码中学到一星半点应用技巧的时候常会有一种发自内心的惊叹,“原来 ...
- [BZOJ5120] [2017国家集训队测试]无限之环
Description 曾经有一款流行的游戏,叫做InfinityLoop,先来简单的介绍一下这个游戏: 游戏在一个n×m的网格状棋盘上进行,其中有些小方格中会有水管,水管可能在方格某些方向的边界的中 ...