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 ...
随机推荐
- poj 3461 hash解法
字符串hash https://blog.csdn.net/pengwill97/article/details/80879387 https://blog.csdn.net/chaiwenjun00 ...
- Spark学习(四)Spark2.3 HA集群的分布式安装
一.下载Spark安装包 1.从官网下载 http://spark.apache.org/downloads.html 2.从微软的镜像站下载 http://mirrors.hust.edu.cn/a ...
- Tomcat7/8开启WebDAV的支持
WebDAV是一种超文本传输协议,Tomcat默认是支持WebDAV的,且默认为禁用状态. 更多详细信息,请参考: https://zh.wikipedia.org/wiki/WebDAV http: ...
- 最大熵推导LR
http://www.win-vector.com/dfiles/LogisticRegressionMaxEnt.pdf https://www.zhihu.com/question/2409455 ...
- 梯度下降和EM算法,kmeans的em推导
I. 牛顿迭代法给定一个复杂的非线性函数f(x),希望求它的最小值,我们一般可以这样做,假定它足够光滑,那么它的最小值也就是它的极小值点,满足f′(x0)=0,然后可以转化为求方程f′(x)=0的根了 ...
- ceph工作原理和安装
一.概述 Ceph是一个分布式存储系统,诞生于2004年,最早致力于开发下一代高性能分布式文件系统的项目.随着云计算的发展,ceph乘上了OpenStack的春风,进而成为了开源社区受关注较高的项目之 ...
- 【剑指offer】打印1到最大的n位数
题目描写叙述: 输入数字n,按顺序打印出从1到最大的n位十进制数.比方输入3,则打印出1.2.3一直到最大的3位数即999. 分析描写叙述: 首先想到的是先计算出最大的n位数是多少,然后用一个循环从1 ...
- C#连接数据库 增删改查
- ansible 基本命令学习与踩坑
1. 命令行参数 -v,–verbose 详细模式,如果命令执行成功,输出详细的结果(-vv –vvv -vvvv) -i PATH,–inventory=PATH 指定host文件的路径,默认是在/ ...
- opencvSGBM半全局立体匹配算法的研究(1)
转载请说明出处:http://blog.csdn.net/zhubaohua_bupt/article/details/51866567 这段时间对opencvSGBM半全局立体匹配算法进行了比較仔细 ...