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 ...
随机推荐
- JS 操作XML
loadXML = function(xmlFile) { var xmlDoc; if(window.ActiveXObject) { xmlDoc ...
- python学习之-requests模块基础
安装版本:2.18 模块导入:import requests l 发送请求 发送GET请求: 获取GITHUB的公共时间线 r = requests.get(url='https://api.git ...
- Bestcoder Tom and matrix
问题描述 Tom放学回家的路上,看到天空中出现一个矩阵.Tom发现,如果矩阵的行.列从0开始标号,第i行第j列的数记为ai,j,那么ai,j=Cji 如果i < j,那么ai,j=0 Tom突发 ...
- Xib/Storyboard碰到不同版本的Xcode真是想死啊!
Command /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/ ...
- 常见的哈希Hash算法 & MD5 & 对称非对称加密 & 海明码
参考 Link 另外,这篇文章也提到了利用Hash碰撞而产生DOS攻击的案例: http://www.cnblogs.com/charlesblc/p/5990475.html DJB的算法实现核心是 ...
- BUPT复试专题—查找(2011)
https://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa?tpId=67&tqId=29646&tPage= ...
- linux系统之shell编程-正則表達式
shell编程正則表達式: 1:元字符 [ ] . * ? + ( ) | { } ^ $ 2 : [a-z0-9] 表示匹配随意数字和字母的一个 3 : [^a-z] ...
- Objective-C之成魔之路【0-序章】
郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 C语言首创 ...
- JQuery插件ajaxFileUpload 异步上传文件
一.先对ajaxFileUpload插件的语法参数进行讲解 原理:ajaxfileupload是通过监听iframe的onload方法来实现, 当从服务端处理完成后,就触发iframe的onload事 ...
- [Pyhton]weakref 弱引用
文档中的解释: https://docs.python.org/2/library/weakref.html wiki 中的解释: 在计算机程序设计中,弱引用.与强引用相对.是指不能确保其引用的对象不 ...