SPOJ:Bits. Exponents and Gcd(组合数+GCD)
Rastas's has been given a number n. Being weak at mathematics, she has to consider all the numbers from 1 to 2n - 1 so as to become perfect in calculations. (You can assume each number is consider as a soldier).
We define the strength of number i as the number of set bits (bits equal to 1) in binary representation of number i.
If the greatest common divisor of numbers a and b is gcd(a, b),
Rastas would like to calculate the function S which is equal to: 
As the friend of Rastas, it's your duty to calculate S modulo 109 + 7.
Input
The first line of the input contains the number of test cases, T. Each of the next T lines contains an integer n, as mentioned in the question
Output
For each value of n given, find the value of the function S.
Constraints
Sum of n over all test cases doesn't exceed 2500.
Example
Input:
3
1
2
5
Output:
0
3
680
题意:给定N,求
,
即对这些(i,j),将i和j表示成二进制,累加i和j的二进制里1的个数的gcd。
思路:考虑靠2^N-1很大,直接针对二进制考虑,因为最多有2500个1,O(N^2)可以暴力搞定。我们考虑组合数,枚举有X个1的个数个Y个1的(i,j),贡献是nun[X]*num[Y]*gcd(X,Y)。当X等于Y时,减去自己。其中num[X]=C(X,N);
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int Mod=1e9+;
int c[],fac[];
int qpow(int a,int x){
a%=Mod; int res=;
while(x){ if(x&) res=(ll)res*a%Mod; a=(ll)a*a%Mod; x>>=; } return res;
}
int main()
{
int N,M,i,j,T,ans;
fac[]=; for(i=;i<=;i++) fac[i]=(ll)fac[i-]*i%Mod;
scanf("%d",&T);
while(T--){
ans=; scanf("%d",&N);
for(i=;i<=N;i++){
c[i]=(ll)fac[N]*qpow(fac[i],Mod-)%Mod*qpow(fac[N-i],Mod-)%Mod;
}
for(i=;i<=N;i++) {
for(j=;j<=N;j++){
if(i!=j) ans=(ans+(ll)c[i]*c[j]%Mod*__gcd(i,j))%Mod;
else ans=(ans+(ll)c[i]*(c[i]-)%Mod*i)%Mod;
}
}
ans=(ll)ans*qpow(,Mod-)%Mod;
printf("%d\n",ans);
}
return ;
}
SPOJ:Bits. Exponents and Gcd(组合数+GCD)的更多相关文章
- UVA 1642 Magical GCD(gcd的性质,递推)
分析:对于区间[i,j],枚举j. 固定j以后,剩下的要比较M_gcd(k,j) = gcd(ak,...,aj)*(j-k+1)的大小, i≤k≤j. 此时M_gcd(k,j)可以看成一个二元组(g ...
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 【CodeForces 803 C】Maximal GCD(GCD+思维)
You are given positive integer number n. You should create such strictly increasingsequence of k pos ...
- UESTC 923 稳住GCD DP + GCD
定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加, dp[i][j] ...
- UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)
题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x ...
- UVa 12716 (GCD == XOR) GCD XOR
题意: 问整数n以内,有多少对整数a.b满足(1≤b≤a)且gcd(a, b) = xor(a, b) 分析: gcd和xor看起来风马牛不相及的运算,居然有一个比较"神奇"的结论 ...
- FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2224 同hdu5869 //#pragma comment(linker, "/STACK:1024 ...
- UVA 11827 Maximum GCD【GCD,stringstream】
这题没什么好说的,但是输入较特别,为此还WA了一次... 题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge& ...
- luoguP2398 GCD SUM [gcd]
题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 给出n求sum. gcd(x,y)表示x,y的最大公约数. 输入输出格式 输入格式: n 输出格式: sum ...
随机推荐
- Day 9 Linux samba & ngnix
(摘) Samba服务 一.Samba简介 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服 ...
- (11)UML设计视图
UML的词汇表包含三种构造块:事物.关系和图 事物:事物是对模型中最具有代表性的成分的抽象 关系:把事物结合在一起 图:图聚集了相关的事物 一.事物 UML中有4种事物 (1)结构事物 UML 模型中 ...
- Debian9初始配置
1 进入root用户 su root 2 修改镜像源:编辑/etc/apt/sources.list文件 nano /etc/apt/sources.list 修改内容如下: deb http://m ...
- Crypto另外两段加密解密的代码
第一段代码风格-平铺直叙: import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex class p ...
- luogu P1074 靶形数独
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...
- PPAPI+Skia实现的涂鸦板
在PPAPI插件中使用Skia画图介绍了怎样在PPAPI中使用Skia,文末说回头要提供一个简单的涂鸦板插件,这次我来兑现承诺了. foruok原创,关注微信订阅号"程序视界"可联 ...
- maven插件介绍之maven-jar-plugin
maven-jar-plugin 插件的maven依赖为: <dependency> <groupId>org.apache.maven.plugins</groupId ...
- boost::serialization 用基类指针转存派生类(错误多多,一波三折)
boost::serialization 也支持c++的多态,这样我们就能够通过使用基类的指针来转存派生类, 我们接着上一篇( boost::serialization(2)序列化基类 )的样例来看: ...
- 宠物连连看2完整Android代码项目
宠物连连看2完整代码,该源代码支持多种风格的连连看游戏的,如有国旗类的连连看,还有宠物连连看的等,主要的功能实现了无尽关卡挑战模式.还有催命倒计时,以及链接提示,暂停.多样图片集,挑战眼力和速度等,而 ...
- 使用JWT设计SpringBoot项目api接口安全服务
转载直: 使用JWT设计SpringBoot项目api接口安全服务