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), ...
随机推荐
- 系统常量对话框QT实现
1.运行结果: 2.代码 main.cpp #include "constantdiag.h" #include <QtWidgets/QApplication> in ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie
题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...
- Java容器之Iterator接口
Iterator 接口: 1. 所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象. 2. Iterator 对象称作迭代器,用以方便的 ...
- 3dContactPointAnnotationTool开发日志(二十)
为了使工具更人性化,我又在每个status的text上绑了个可以拖拽实现值改变的脚本,但是不知道为啥rotx那个值越过+-90范围后连续修改就会产生抖动的现象,试了很多方法也没能弄好,不过实际用起 ...
- thinkphp5 隐藏入口和支持pathinfo
url里public目录的隐藏 其实正常思路的话这个url里的public本身就是不存在的,然后呢,其实也不叫隐藏public目录,这里只是考虑到有些童鞋可能还会按之前3.x时代的习惯来配置网站根目录 ...
- 【Linux】- netstat 命令
Linux netstat命令用于显示网络状态.利用netstat指令可让你得知整个Linux系统的网络情况. 语法 netstat [-acCeFghilMnNoprstuvVwx][-A<网 ...
- jdbc关闭连接顺序
jdbc连接数据库时,先获取connection,再通过statement进行操作,将结果集放在resultset中,不过在关闭数据库的时候要小心,要跟前面的操作反着来,不然就会出现异常.如果直接关闭 ...
- 给MySQL字段添加索引的操作
1.添加PRIMARY KEY(主键索引): ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE(唯一索引) : ALTE ...
- springBoot按条件装配:Condition
编码格式转换器接口 package com.boot.condition.bootconditionconfig.converter; /** * 编码格式转换器接口 */ public interf ...
- BZOJ 1010 玩具装箱(斜率优化DP)
dp[i]=min(dp[j]+(sum[i]-sum[j]+i-j-1-L)^2) (j<i) 令f[i]=sum[i]+i,c=1+l 则dp[i]=min(dp[j]+(f[i]-f[j] ...