1. 程式人生 > >SPOJ:Bits. Exponents and Gcd(組合數+GCD)

SPOJ:Bits. Exponents and Gcd(組合數+GCD)

force test each represent tro 其中 bit loaded calc

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+7;
int c[2510],fac[2510];
int qpow(int a,int x){
    a%=Mod; int res=1;
    while(x){ if(x&1) res=(ll)res*a%Mod; a=(ll)a*a%Mod; x>>=1; } return res;
}
int main()
{
    int N,M,i,j,T,ans;
    fac[0]=1; for(i=1;i<=2500;i++) fac[i]=(ll)fac[i-1]*i%Mod;
    scanf("%d",&T);
    while(T--){
        ans=0; scanf("%d",&N);
        for(i=1;i<=N;i++){
            c[i]=(ll)fac[N]*qpow(fac[i],Mod-2)%Mod*qpow(fac[N-i],Mod-2)%Mod;
        }
        for(i=1;i<=N;i++) {
            for(j=1;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]-1)%Mod*i)%Mod;
            }
        }
        ans=(ll)ans*qpow(2,Mod-2)%Mod;
        printf("%d\n",ans);
    }
    return 0;
}

SPOJ:Bits. Exponents and Gcd(組合數+GCD)