链接
https://acm.hdu.edu.cn/showproblem.php?pid=4768
题意
有 n 个社团,每个社团给学号为 $a_i+kc_i$ $(a_i+k*c_i<=b_i,a_i+(k+1)*c_i>b_i)$ 的学生一张传单,最多只有一个学生最终拥有奇数张传单,找出这个学生。
思路
因为最多一个数有奇数个,假设这个数为 $x$,那么小于 $x$ 的数的前缀和一定是偶数,大于等于 $x$ 的数的前缀和一定是奇数,二分即可。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
   | #include <bits/stdc++.h> #define SZ(x) (int)(x).size() #define ALL(x) (x).begin(),(x).end() #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second using namespace std; typedef double DB; typedef long double LD; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VPII;
  const int N=2e4+5; int n; LL a[N],b[N],c[N]; bool check(LL mid) {     LL tot=0;     for(int i=1;i<=n;i++) {         LL d=(min(b[i],mid)-a[i]);         if(d<0) continue;         tot+=d/c[i]+1;     }     return tot&1; } int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     while(cin>>n) {         for(int i=1;i<=n;i++) cin>>a[i]>>b[i]>>c[i];         LL l=1,r=1ll<<32;         while(l<r) {             LL mid=l+r>>1;             if(check(mid)) r=mid;             else l=mid+1;         }         if(l==1ll<<32) cout<<"DC Qiang is unhappy.\n";         else {             LL res=0;             for(int i=1;i<=n;i++) {                 LL d1=(min(b[i],l)-a[i])/c[i];                 if(d1>=0) res+=d1+1;                 LL d2=(min(b[i],l-1)-a[i])/c[i];                 if(d2>=0) res-=d2+1;             }             cout<<l<<' '<<res<<'\n';         }     }     return 0; }
   |