Showstopper
Data-mining huge data sets can be a painful and long lasting process if we are not aware of tiny patterns existing within those data sets.
One reputable company has recently discovered a tiny bug in their hardware video processing solution and they are trying to create software workaround. To achieve maximum performance they use their chips in pairs and all data objects in memory should have even number of references. Under certain circumstances this rule became violated and exactly one data object is referred by odd number of references. They are ready to launch product and this is the only showstopper they have. They need YOU to help them resolve this critical issue in most efficient way.
Can you help them?
Input
Input file consists from multiple data sets separated by one or more empty lines.
Each data set represents a sequence of 32-bit (positive) integers (references) which are stored in compressed way.
Each line of input set consists from three single space separated 32-bit (positive) integers X Y Z and they represent following sequence of references: X, X+Z, X+2 * Z, X+3 * Z, …, X+K * Z, …(while (X+K*Z)<=Y).
Your task is to data-mine input data and for each set determine weather data were corrupted, which reference is occurring odd number of times, and count that reference.
Output
For each input data set you should print to standard output new line of text with either “no corruption” (low case) or two integers separated by single space (first one is reference that occurs odd number of times and second one is count of that reference).
题意 + 题解
每个数会被应用偶数次(包括0),只存在一个数被引用奇数次。
例如:
1 10 2
4 4 1
1 9 3
3 5 2
可以理解为
此时只有9是被奇数次引用的。
答案为: 9 1
再举一个例子
1 12 2
4 4 1
1 9 3
3 11 2
此时 7 是奇数次引用。
可以发现 在 7以前的 所有数引用的次数都是偶数。
例如 在 6之前 1引用了2次,3引用了2次,4引用了2次,5引用了2次.
一共引用里8次。而在7以后,由于 7 引用了奇数次, 那么后面就不会有引用奇数次的数了,后面的引用次数和一定为 奇数 。
问题 就变成为 找第一个引用次数为奇数的那个数字,还有其引用次数
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<sstream>
using namespace std;
int const MAX = 5e5;
long long x[MAX], y[MAX], z[MAX];
int n;
/*这个是我在网上看到的一种解法,代码量少,但是不容易理解(下面是强行解释的)
long long check(long long mid) {
long long sum = 0;
for(long long i = 0; i < n; i++) {
if(mid < x[i]) continue;
sum = sum + (min(y[i], mid) - x[i]) / z[i] + 1;
//+1是因为4 4 1这种情况 0 / 1 = 0,所以要 +1
//此时第1,3,4行多加了1,而第二行则没有
//如果min(y[i], mid) - x[i]不等于0,则每行都+1,而行数是偶数,不影响奇偶性
}
return sum;
}
*/
long long check(long long mid) {
long long sum = 0;
for(long long i = 0; i < n; i++) {
if(mid < x[i]) continue;
long long t = min(y[i], mid) - x[i] + 1;//两个端点都算进去的 1到3,其实有三个数字
sum = sum + (t + z[i] - 1)/ z[i]; //向上取整 3 11 2 九个数里面都5个2的倍数,而不是9/2 = 4个
}
return sum;
}
long long check(long long mid) {
long long sum = 0;
for(long long i = 0; i < n; i++) {
if(mid < x[i]) continue;
long long t = min(y[i], mid) - x[i] + 1;
if(t == 0) sum++;
else sum = sum + (t + z[i] - 1)/ z[i];
}
return sum;
}
void solve() {
long long L = 0, R = 1LL << 33;
while(R - L > 1) {
long long mid = (L + R) / 2;
if(check(mid) % 2 == 0) L = mid;
else R = mid;
}
//cout << L << " " << R << endl;
if(R == 1LL << 33) printf("no corruption\n");
//no corruption 即引用次数和都是偶数 那么R就不会变小
else printf("%lld %lld\n", R , check(R) - check(R - 1));
}
int main() {
char a[100];
n = 0;
while(gets(a)) {
if(strlen(a)) {
sscanf(a, "%lld %lld %lld", &x[n], &y[n], &z[n]);
n++;
}
if(!strlen(a) && n) {
solve();
n = 0;
}
}
if(n) solve();
}