DreamGrid has a nonnegative integer . He would like to divide  into nonnegative integers  and minimizes their bitwise or (i.e.  and  should be as small as possible).

Input

There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers  and  ().

It is guaranteed that the sum of the length of  does not exceed .

<h4< dd="">Output

For each test case, output an integer denoting the minimum value of their bitwise or.

<h4< dd="">Sample Input

5
3 1
3 2
3 3
10000 5
1244 10

<h4< dd="">Sample Output

3
3
1
2000
125
题解:题意很简单,就是让你把n分成m份,然后让你求这m份按位或的最小值;(注意数据范围,大数模板考虑下Orz)
考虑一个k满足m*2^k <= n < m*2^(k+1)如果使得结果最小,则对于分开后,每个数的最高位(二进制)位置越小,找到一个k后,我们让这m个数字第k位都为一。
然后剩下n-m*2^k(相当于新的n),递归求解即可;
参考代码:
 #include <bits/stdc++.h>
using namespace std;
// base and base_digits must be consistent
constexpr int base = ;
constexpr int base_digits = ;
struct bigint
{
vector<int> z;
int sign;
bigint() : sign() {}
bigint(long long v)
{
*this = v;
}
bigint& operator=(long long v)
{
sign = v < ? - : ;
v *= sign;
z.clear();
for(; v > ; v = v / base) z.push_back((int)(v % base));
return *this;
} bigint(const string& s)
{
read(s);
} bigint& operator+=(const bigint& other)
{
if (sign == other.sign)
{
for (int i = , carry = ; i < other.z.size() || carry; ++i)
{
if(i == z.size()) z.push_back();
z[i] += carry + (i < other.z.size() ? other.z[i] : );
carry = z[i] >= base;
if(carry) z[i] -= base;
}
}
else if (other != /* prevent infinite loop */)
{
*this -= -other;
}
return *this;
} friend bigint operator+(bigint a, const bigint& b)
{
return a += b;
} bigint& operator-=(const bigint& other)
{
if (sign == other.sign)
{
if (sign == && *this >= other || sign == - && *this <= other)
{
for (int i = , carry = ; i < other.z.size() || carry; ++i)
{
z[i] -= carry + (i < other.z.size() ? other.z[i] : );
carry = z[i] < ;
if(carry) z[i] += base;
}
trim();
}
else
{
*this = other - *this;
this->sign = -this->sign;
}
}
else *this += -other;
return *this;
} friend bigint operator - (bigint a, const bigint& b)
{
return a -= b;
} bigint& operator*=(int v)
{
if(v < ) sign = -sign, v = -v;
for(int i = , carry = ; i < z.size() || carry; ++i)
{
if(i == z.size()) z.push_back();
long long cur = (long long)z[i] * v + carry;
carry = (int)(cur / base);
z[i] = (int)(cur % base);
}
trim();
return *this;
} bigint operator*(int v) const
{
return bigint(*this) *= v;
} friend pair<bigint, bigint> divmod(const bigint& a1, const bigint& b1)
{
int norm = base / (b1.z.back() + );
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.z.resize(a.z.size()); for (int i = (int)a.z.size() - ; i >= ; i--)
{
r *= base;
r += a.z[i];
int s1 = b.z.size() < r.z.size() ? r.z[b.z.size()] : ;
int s2 = b.z.size() - < r.z.size() ? r.z[b.z.size() - ] : ;
int d = (int)(((long long)s1 * base + s2) / b.z.back());
r -= b * d;
while(r < ) r += b, --d;
q.z[i] = d;
} q.sign = a1.sign * b1.sign;
r.sign = a1.sign;
q.trim();
r.trim();
return {q, r / norm};
} friend bigint sqrt(const bigint& a1)
{
bigint a = a1;
while(a.z.empty() || a.z.size() % == ) a.z.push_back(); int n = a.z.size();
int firstDigit = (int)::sqrt((double)a.z[n - ] * base + a.z[n - ]);
int norm = base / (firstDigit + );
a *= norm;
a *= norm;
while(a.z.empty() || a.z.size() % == ) a.z.push_back(); bigint r = (long long)a.z[n - ] * base + a.z[n - ];
firstDigit = (int)::sqrt((double)a.z[n - ] * base + a.z[n - ]);
int q = firstDigit;
bigint res;
for (int j = n / - ; j >= ; j--)
{
for(;; --q)
{
bigint r1 = (r - (res * * base + q) * q) * base * base + (j > ? (long long)a.z[ * j - ] * base + a.z[ * j - ] : );
if(r1 >= )
{
r = r1;
break;
}
}
res *= base;
res += q;
if(j > )
{
int d1 = res.z.size() + < r.z.size() ? r.z[res.z.size() + ] : ;
int d2 = res.z.size() + < r.z.size() ? r.z[res.z.size() + ] : ;
int d3 = res.z.size() < r.z.size() ? r.z[res.z.size()] : ;
q = (int)(((long long)d1 * base * base + (long long)d2 * base + d3) / (firstDigit * ));
}
} res.trim();
return res / norm;
} bigint operator/(const bigint& v) const
{
return divmod(*this, v).first;
} bigint operator%(const bigint& v) const
{
return divmod(*this, v).second;
} bigint& operator/=(int v)
{
if(v < ) sign = -sign, v = -v;
for (int i = (int)z.size() - , rem = ; i >= ; --i)
{
long long cur = z[i] + rem * (long long)base;
z[i] = (int)(cur / v);
rem = (int)(cur % v);
}
trim();
return *this;
} bigint operator/(int v) const
{
return bigint(*this) /= v;
} int operator%(int v) const
{
if(v < ) v = -v;
int m = ;
for(int i = (int)z.size() - ; i >= ; --i) m = (int)((z[i] + m * (long long)base) % v);
return m * sign;
} bigint& operator*=(const bigint& v)
{
*this = *this * v;
return *this;
} bigint& operator/=(const bigint& v)
{
*this = *this / v;
return *this;
} bool operator<(const bigint& v) const
{
if(sign != v.sign) return sign < v.sign;
if(z.size() != v.z.size()) return z.size() * sign < v.z.size() * v.sign;
for(int i = (int)z.size() - ; i >= ; i--)
if(z[i] != v.z[i]) return z[i] * sign < v.z[i] * sign;
return false;
} bool operator>(const bigint& v) const
{
return v < *this;
}
bool operator<=(const bigint& v) const
{
return !(v < *this);
}
bool operator>=(const bigint& v) const
{
return !(*this < v);
}
bool operator==(const bigint& v) const
{
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint& v) const
{
return *this < v || v < *this;
} void trim()
{
while(!z.empty() && z.back() == ) z.pop_back();
if(z.empty()) sign = ;
} bool isZero() const
{
return z.empty();
} friend bigint operator-(bigint v)
{
if(!v.z.empty()) v.sign = -v.sign;
return v;
} bigint abs() const
{
return sign == ? *this : -*this;
} long long longValue() const
{
long long res = ;
for(int i = (int)z.size() - ; i >= ; i--) res = res * base + z[i];
return res * sign;
} friend bigint gcd(const bigint& a, const bigint& b)
{
return b.isZero() ? a : gcd(b, a % b);
} friend bigint lcm(const bigint& a, const bigint& b)
{
return a / gcd(a, b) * b;
} void read(const string& s)
{
sign = ;
z.clear();
int pos = ;
while(pos < s.size() && (s[pos] == '-' || s[pos] == '+'))
{
if(s[pos] == '-') sign = -sign;
++pos;
}
for(int i = (int)s.size() - ; i >= pos; i -= base_digits)
{
int x = ;
for(int j = max(pos, i - base_digits + ); j <= i; j++) x = x * + s[j] - '';
z.push_back(x);
}
trim();
} friend istream& operator>>(istream& stream, bigint& v)
{
string s;
stream >> s;
v.read(s);
return stream;
} friend ostream& operator<<(ostream& stream, const bigint& v)
{
if(v.sign == -) stream << '-';
stream << (v.z.empty() ? : v.z.back());
for(int i = (int)v.z.size() - ; i >= ; --i)
stream << setw(base_digits) << setfill('') << v.z[i];
return stream;
} static vector<int> convert_base(const vector<int>& a, int old_digits, int new_digits)
{
vector<long long> p(max(old_digits, new_digits) + );
p[] = ;
for(int i = ; i < p.size(); i++) p[i] = p[i - ] * ;
vector<int> res;
long long cur = ;
int cur_digits = ;
for(int v : a)
{
cur += v * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits)
{
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int)cur);
while(!res.empty() && res.back() == )
res.pop_back();
return res;
} typedef vector<long long> vll;
static vll karatsubaMultiply(const vll& a, const vll& b)
{
int n = a.size();
vll res(n + n);
if(n <= )
{
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
} int k = n >> ;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());
vll a1b1 = karatsubaMultiply(a1, b1);
vll a2b2 = karatsubaMultiply(a2, b2);
for(int i = ; i < k; i++) a2[i] += a1[i];
for(int i = ; i < k; i++) b2[i] += b1[i]; vll r = karatsubaMultiply(a2, b2);
for(int i = ; i < a1b1.size(); i++) r[i] -= a1b1[i];
for(int i = ; i < a2b2.size(); i++) r[i] -= a2b2[i];
for(int i = ; i < r.size(); i++) res[i + k] += r[i];
for(int i = ; i < a1b1.size(); i++) res[i] += a1b1[i];
for(int i = ; i < a2b2.size(); i++) res[i + n] += a2b2[i];
return res;
} bigint operator*(const bigint& v) const
{
vector<int> a6 = convert_base(this->z, base_digits, );
vector<int> b6 = convert_base(v.z, base_digits, );
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while(a.size() < b.size()) a.push_back();
while(b.size() < a.size()) b.push_back();
while(a.size() & (a.size() - )) a.push_back(), b.push_back();
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = , carry = ; i < c.size(); i++)
{
long long cur = c[i] + carry;
res.z.push_back((int)(cur % ));
carry = (int)(cur / );
}
res.z = convert_base(res.z, , base_digits);
res.trim();
return res;
}
};
/***********************************************
上面为大数模板 核心代码
************************************************/
int main()
{
ios::sync_with_stdio();
cin.tie();
bigint n, m;
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
bigint ans = ;
bigint now = ;
while(now<= n)
{
now = now * ;
}
while(n != )
{
while(now != && now * m > n)
{
now = now / ;
}
if((now * - ) * m < n)
now = now * ;
bigint num = n / now;
if(num > m)
num = m;
n = n - num * now;
ans = ans + now;
}
cout << ans << endl;
}
return ;
}
  

2017 CCPC秦皇岛 G题 Numbers的更多相关文章

  1. 2017 CCPC秦皇岛 A题 A Ballon Robot

    The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be  teams parti ...

  2. 2017 CCPC秦皇岛 M题 Safest Buildings

    PUBG is a multiplayer online battle royale video game. In the game, up to one hundred players parach ...

  3. 2017 CCPC秦皇岛 L题 One Dimensions Dave

    BaoBao is trapped in a one-dimensional maze consisting of  grids arranged in a row! The grids are nu ...

  4. 2017 CCPC秦皇岛 E题 String of CCPC

    BaoBao has just found a string  of length  consisting of 'C' and 'P' in his pocket. As a big fan of ...

  5. 2017CCPC秦皇岛 G题Numbers&&ZOJ3987【大数】

    题意: 给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n,并且要使得这m个数的或值最小. 思路: 从二进制的角度分析,如果这m个数中有一个数某一位为1,那么最后或起来这一位肯定是为1的, ...

  6. 2017 CCPC秦皇岛 H题 Prime set

    Given an array of  integers , we say a set  is a prime set of the given array, if  and  is prime. Ba ...

  7. 2017 ccpc哈尔滨 A题 Palindrome

    2017 ccpc哈尔滨 A题 Palindrome 题意: 给一个串\(T\),计算存在多少子串S满足\(S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)\) 思路: 很明显这里的回文串长 ...

  8. HDU 6268 Master of Subgraph (2017 CCPC 杭州 E题,树分治 + 树上背包)

    题目链接  2017 CCPC Hangzhou  Problem E 题意  给定一棵树,每个点有一个权值,现在我们可以选一些连通的点,并且把这点选出来的点的权值相加,得到一个和. 求$[1, m] ...

  9. HDU 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)

    题目链接  2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块.    分块的时候满足每个块是一个 ...

随机推荐

  1. PowerDesigner列名、注释内容互换

    资料来源:PowerDesigner列名.注释内容互换 文中一共提供了2种操作的代码. (1)将Name中的字符COPY至Comment中 (2)将Comment中的字符COPY至Name中 使用方法 ...

  2. C# VII: 统计文本行数

    本文基于StackOverflow的以下问题收集整理而成. What is the fastest waty to count newlines in a large .NET string: htt ...

  3. spark安装配置

    一.下载解压 二.配置 (假设已经配置了Java.Hadoop) 1.环境变量 2.spark配置 进入spark安装目录,复制文件 编辑spark-env.sh文件,在文件中添加如下信息(括号中路径 ...

  4. 05-商品类别数据和VUE展示

    一.商品类别数据和VUE展示 1.商品类别数据接口 将商品类别数据展示出来,视图(views.py)代码如下: class CategoryViewset(mixins.ListModelMixin, ...

  5. nyoj 255-C小加 之 随机数 (map)

    255-C小加 之 随机数 内存限制:64MB 时间限制:3000ms 特判: No 通过数:15 提交数:18 难度:1 题目描述: ACM队的“C小加”同学想在学校中请一些同学一起做一项问卷调查, ...

  6. 一文教你快速读懂MQTT网关

    MQTT是一种发布(publish)/订阅(subscribe)协议,MQTT协议采用发布/订阅模式,所有的物联网终端都通过TCP连接到云端,云端通过主题的方式管理各个设备关注的通讯内容,负责将设备与 ...

  7. Java——内部类详解

    说起内部类,大家肯定感觉熟悉又陌生,因为一定在很多框架源码中有看到别人使用过,但又感觉自己使用的比较少,今天我就带你具体来看看内部类. 内部类基础 所谓内部类就是在类的内部继续定义其他内部结构类. 在 ...

  8. 解决django或者其他线程中调用scrapy报ReactorNotRestartable的错误

    官网中关于ReactorNotRestartable的错误描述(摘自:https://twistedmatrix.com/documents/16.1.0/api/twisted.internet.e ...

  9. 【Elasticsearch 7 探索之路】(四)Analyzer 分析

    上一篇,什么是倒排索引以及原理是什么.本篇讲解 Analyzer,了解 Analyzer 是什么 ,分词器是什么,以及 Elasticsearch 内置的分词器,最后再讲解中文分词是怎么做的. 一.A ...

  10. 2019-9-19:渗透测试,基础学习,html常用标签,笔记

    HTML常用标签<br>:换行<meta charset="utf-8">:编码标签<script type="路径">:插 ...