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 ;
}
最新文章
- penmount串口触摸屏加载
- QueryInterface
- 通过ajax获得json数据后格式的转换
- Effective C++ Item 37 绝不又一次定义继承而来的缺省參数值
- hdu 1358 period KMP入门
- struts2入门
- 解决Qt程序在Linux下无法输入中文的办法
- oracle乱码问题
- PostgreSQL服务端监听设置及client连接方法
- jq向webApi提交post json数据
- Redis应用----消息传递
- .NET中webservice如何使用,调用
- Flunetd 用于统一日志记录层的开源数据收集器
- 基于 Vue.js 之 iView UI 框架非工程化实践记要
- dubbo源码—SPI
- Linux:sheel脚本for的用法,及日期参数+1day用法
- Jetson TX1 SD card启动
- 洛谷P1402 酒店之王
- 人人中的 shiro权限管理 简单说明
- Java高级特性 第6节 注解初识
热门文章
- 【Java TCP/IP Socket】Java NIO Socket VS 标准IO Socket
- python内存泄露诊断过程记录pyrasite
- Java面试题集(151-180)
- 关于Memcached的CAS和Set方法造成Socket泄漏的问题
- 【转载】究竟啥才是互联网架构&ldquo;高可用&rdquo;
- Cocoapods Undefined symbols for architecture armv7s\arm64
- 服务器----1U、2U、3U、4U
- iOS 相似淘宝商品详情查看翻页效果的实现
- 前端编程提高之旅(五)----写给大家看的css书
- 代码调试过程中easy遇到的问题