2019牛客多校九 I. KM and M (类欧几里得)
大意: 给定$N,M$, 求$\sum\limits_{K=1}^N \text{(KM)&M}$
考虑第$i$位的贡献, 显然为$\lfloor\frac{KM}{2^i}\rfloor$为奇数的个数再乘上$2^i$
也就等于$2^i(\sum \lfloor\frac{KM}{2^i}\rfloor-2\sum \lfloor\frac{KM}{2^{i+1}}\rfloor)$, 可以用类欧求出
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, inv2 = (P+1)/2;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head int n, m;
int solve(ll a, ll b, ll c, ll n) {
if (!a) return (n+1)*(b/c)%P;
if (a>=c||b>=c) {
int t = solve(a%c,b%c,c,n); n %= P;
return (t+(a/c)%P*n%P*(n+1)%P*inv2+(b/c)%P*(n+1))%P;
}
ll m = ((__int128)a*n+b)/c;
return ((n%P)*(m%P)-solve(c,c-b-1,a,m-1))%P;
} int main() {
ll n, m;
cin>>n>>m;
int ans = 0;
REP(i,0,60) if (m>>i&1) {
int A = solve(m,0,1ll<<i,n);
int B = solve(m,0,1ll<<i+1,n);
int C = (1ll<<i)%P;
ans = (ans+(A-2ll*B)*C)%P;
}
if (ans<0) ans += P;
printf("%d\n", ans);
}
2019牛客多校九 I. KM and M (类欧几里得)的更多相关文章
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- 2019牛客多校第二场 A Eddy Walker(概率推公式)
2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
- 2019牛客多校 Round4
Solved:3 Rank:331 B xor 题意:5e4个集合 每个集合最多32个数 5e4个询问 询问l到r个集合是不是都有一个子集的xor和等于x 题解:在牛客多校第一场学了线性基 然后这个题 ...
- 2019牛客多校第一场E ABBA(DP)题解
链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 ABBA 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语 ...
- 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数
目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...
- 2019牛客多校第四场 A meeting
链接:https://ac.nowcoder.com/acm/contest/884/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10485 ...
- [2019牛客多校第二场][G. Polygons]
题目链接:https://ac.nowcoder.com/acm/contest/882/G 题目大意:有\(n\)条直线将平面分成若干个区域,要求处理\(m\)次询问:求第\(q\)大的区域面积.保 ...
- 2019 牛客多校第一场 D Parity of Tuples
题目链接:https://ac.nowcoder.com/acm/contest/881/D 看此博客之前请先参阅吕凯飞的论文<集合幂级数的性质与应用及其快速算法>,论文中很多符号会被本文 ...
随机推荐
- STM32 软件按键消抖
引言 通常按键所用的开关都是机械弹性开关,当机械触点断开.闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上就稳定的接通,在断开时也不会一下子彻底断开,而是在闭合和断开的瞬间伴随了一连串的抖 ...
- GPS 经纬度
经纬度地图: http://www.gpsspg.com/maps.htm http://www.gzhatu.com/dingwei.html 经纬度格式转化 http://www.gzhatu ...
- post请求头中常见content-type(非常重要)
定义和用法 enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码.默认地,表单数据会编码为 "application/x-www-form-urlencoded". ...
- nginx: [warn] conflicting server name "aaa.bbbb.com" on 0.0.0.0:80, ignored
date: 2019-08-12 16:52:44 author: headsen chen notice :个人原创 故障现象: openresty -t nginx: [warn] confli ...
- IDEA分配内存无效
idea改启动内存分配, 改 C:/Users/xxx/.IntelliJIdea2018.1/confing/idea64.exe.vmoptions 或 C:/Users/xxx/.Intelli ...
- 逐层解析请求json参数字符串【我】
import net.sf.json.JSONObject; 逐层解析请求json参数字符串: InputStream inStream =null; BufferedReader br =null; ...
- shop++上传图片失败
如题上传shop++商城商品图片失败. 其主要原因是图片过大.本人使用测试需要在500KB以下才能成功 1.点击编辑缩小尺寸.这个缩小有限 2.用QQ截图另存为png
- NodeVisitor
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library is open source ...
- python哲学
import this The Zen of Python, by Tim Peters # python的设计哲学, 作者:Tim Peters Beautiful is better than u ...
- 算法习题---5-8图书管理系统*****<双向迭代器>(UVa230)
一:题目 就是输入一系列书本名和作者名,然后输入命令模拟借书和还书,再输出归还的书的摆放位置.要求有两点: 需要对归还的书做特殊排序处理:作者名相同,则书本按书名从小到大排序:否则书本按作者名大小排序 ...