大意: 给定$N,M$, 求$sumlimits_{K=1}^N ext{(KM)&M}$
考虑第$i$位的贡献, 显然为$lfloorfrac{KM}{2^i} floor$为奇数的个数再乘上$2^i$
也就等于$2^i(sum lfloorfrac{KM}{2^i} floor-2sum lfloorfrac{KM}{2^{i+1}} floor)$, 可以用类欧求出
#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 ' ' #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 ", ans); }