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:
5
Output: 
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)的更多相关文章

  1. 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 ...

  2. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  3. 【CodeForces 803 C】Maximal GCD(GCD+思维)

    You are given positive integer number n. You should create such strictly increasingsequence of k pos ...

  4. UESTC 923 稳住GCD DP + GCD

    定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加,  dp[i][j] ...

  5. 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 ...

  6. UVa 12716 (GCD == XOR) GCD XOR

    题意: 问整数n以内,有多少对整数a.b满足(1≤b≤a)且gcd(a, b) = xor(a, b) 分析: gcd和xor看起来风马牛不相及的运算,居然有一个比较"神奇"的结论 ...

  7. FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2224 同hdu5869 //#pragma comment(linker, "/STACK:1024 ...

  8. UVA 11827 Maximum GCD【GCD,stringstream】

    这题没什么好说的,但是输入较特别,为此还WA了一次... 题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge& ...

  9. 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 ...

随机推荐

  1. ZOJ - 4019 Schrödinger's Knapsack (背包,贪心,动态规划)

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5747 [题目大意]:薛定谔的背包.薛定谔的猫是只有观测了才知道猫的死 ...

  2. Java利用Mybatis进行数据权限控制

    权限控制主要分为两块,认证(Authentication)与授权(Authorization).认证之后确认了身份正确,业务系统就会进行授权,现在业界比较流行的模型就是RBAC(Role-Based ...

  3. BIM

    BIM进入中国已经有十来个年头,随着对BIM概念的深入了解.当前国内BIM应用逐渐由三维模型的可视化应用升级为基于BIM模型的信息进行项目精细化动态管理. 传统粗放的项目管理方法是工程项目难以进行精细 ...

  4. Go -- go语言指针

    package main import "fmt" type Test struct { Name string } func change2(t *Test) { t.Name ...

  5. 一个球,初始高度100,每次落下回弹一半高度,求第n次落下球走的距离

    def get_height(n): if n==1: eturn 150 return 100+sum([200*pow(0.5,i) for i in range(1,n)])+100*pow(0 ...

  6. influxdb的python操作

    1.先安装依赖:pip install influxdb 2.

  7. 阿里云OSS 图片处理api(custom)

    阿里云OSS 图片处理api(custom) 阿里云对象存储服务(Object Storage Service, 简称OSS) 学习了:https://blog.csdn.net/u014559227 ...

  8. ZOJ ACM 1314(JAVA)

    昨天做了几个题目.过于简单,就不在博客里面写了. 1314这道题也比較简单,写出来是由于我认为在这里有一个小技巧,对于时间复杂度和空间复杂度都比較节省. 这个题目类似哈希表的求解.可是更简单.刚拿到题 ...

  9. Our happy ending

    题目链接 题意: 输入n.k.L,n个数,最大值不超过L,在序列中取若干个数和能达到k的序列个数 n,k<=20 , 0<=L<=10^9 分析: 题目关键在于和k比較小,所以能够考 ...

  10. react 创建组件 (二)component

    因为ES6对类和继承有语法级别的支持,所以用ES6创建组件的方式更加优雅,下面是示例: import React from 'react'; class Greeting extends React. ...