• hdu6275 Mod, Xor and Everything(分块,类欧几里得)


    时间限制: 10 Sec  内存限制: 128 MB
    提交: 198  解决: 10
    [提交] [状态] [命题人:admin]

    题目描述

    You are given an integer n.
    You are required to calculate (n mod 1) xor (n mod 2) xor ... xor (n mod (n - 1)) xor (n mod n).
    The “xor” operation means “exclusive OR”.

    输入

    The first line contains an integer T (1≤T≤5) representing the number of test cases.
    For each test case, there is an integer n (1≤n≤1012) in one line.

    输出

    For each test case, print the answer in one line.

    样例输入

    5
    1
    2
    3
    4
    5
    

    样例输出

    0
    0
    1
    1
    2


    按位求出每一位的值,从左到右第k位为
    然后用类欧算分块的每一块(类欧几里得算法小结
     

    $sum_{i=1}^{n} lfloor frac{n \% i}{2^k} floor =
    sum_{i=1}^{n} lfloor frac{n - lfloor frac{n}{i} floor i}{2^k} floor$

     


    #include "bits/stdc++.h"
    
    using namespace std;
    typedef long long ll;
    
    bool f(ll a, ll b, ll c, ll n) {
        if (!a) return (((n + 1) & (b / c)) & 1ll) > 0;
        if (a >= c || b >= c) {
            ll temp = (n & 1ll) ? (n + 1) / 2 * n : n / 2 * (n + 1);//先除后乘防止溢出
            return ((a / c * temp + (b / c) * (n + 1) + f(a % c, b % c, c, n)) & 1ll) > 0;
        } else {
            ll m = (a * n + b) / c;
            return (((n * m) ^ f(c, c - b - 1, a, m - 1)) & 1) > 0;
        }
    }
    
    int main() {
        int _;
        scanf("%d", &_);
        while (_--) {
            ll n;
            scanf("%lld", &n);
            ll ans = 0, to = min(30000000ll, n);
            for (ll i = 1; i < to; i++) {
                ans = ans ^ (n % i);
            }
            for (ll i = to, j; i <= n; i = j + 1) {
                j = n / (n / i);
                ll c = 1, ans1 = 0;
                for (int k = 1; k <= 50; k++) {
                    ans1 += f(n / i, n % j, c, j - i) * c;
                    c <<= 1;
                }
                ans ^= ans1;
            }
            printf("%lld
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    phpcms 短信替换
    phpcms 搭建宣传网站首页
    JDK 8
    MySQL 5.6 date 与 string 的转换和比较
    Illustration of Git branching and merge
    Anti-pattern(反模式)
    Design Patterns笔记
    MyBatis小抄
    MyBatis MapperScannerConfigurer
    MyBatis 批量插入数据对插入记录数的限制
  • 原文地址:https://www.cnblogs.com/albert-biu/p/10686175.html
Copyright © 2020-2023  润新知