CF--839EMother of Dragons--容斥原理+数学+算贡献

There are n castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more than one wall, no wall connects a castle to itself.

Sir Jaime Lannister has discovered that Daenerys Targaryen is going to attack his kingdom soon. Therefore he wants to defend his kingdom. He has k liters of a strange liquid. He wants to distribute that liquid among the castles, so each castle may contain some liquid (possibly zero or non-integer number of liters). After that the stability of a wall is defined as follows: if the wall connects two castles a and b, and they contain x and y liters of that liquid, respectively, then the strength of that wall is x·y.

Your task is to print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 40, 1 ≤ k ≤ 1000).

Then n lines follows. The i-th of these lines contains n integers ai, 1, ai, 2, ..., ai, n (). If castles i and j are connected by a wall, then ai, j = 1. Otherwise it is equal to 0.

It is guaranteed that ai, j = aj, i and ai, i = 0 for all 1 ≤ i, j ≤ n.

Output

Print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Examples

Input

3 1
0 1 0
1 0 0
0 0 0

Output

0.250000000000

Input

4 4
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0

Output

4.000000000000

Note

In the first sample, we can assign 0.5, 0.5, 0 liters of liquid to castles 1, 2, 3, respectively, to get the maximum sum (0.25).

In the second sample, we can assign 1.0, 1.0, 1.0, 1.0 liters of liquid to castles 1, 2, 3, 4, respectively, to get the maximum sum (4.0)

 

给出一组数,从中任意选几个,若他们的gcd>1,那么贡献值为这几个数的个数*gcd。

思路:容斥原理。枚举因子,对于每一个因子i(i>1), 我们统计它的倍数的个数,同时我们会计算了2*i,3*i....等的倍数个数。从他们的倍数中挑选,即可挑出gcd为i,2i,3i....的序列,计算他们的贡献(需要去重)。

若i的个数为num,即cnt[i],它的贡献为i*(1*C(num,1)+2*C(num,2)+...+num*C(num,num))。这个式子可以用一个数学公式来计算,即i*num*2^(num-1)。利用快速幂计算即可。然后我们要考虑第一步的去重,因为计算i的倍数的时候,多算了2i...这样的,要去重,用f[i]来表示他们的贡献,则f[i]=f[i]-f[i*2]-f[i*3]-.....。这时们从大往小枚举,即可计算,最后*i取模累加即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=200000+66;
const ll mod=1e9+7;
ll n,k;
ll a[maxn];
ll cnt[maxn*10];
ll f[maxn*10];
ll pow_quick(ll a,ll b,ll p)
{
    ll ans=1%p;
    for(; b; b>>=1)
    {
        if(b&1)
            ans=(ans*a)%p ;
        a=a*a%p ;
    }
    return ans;
}
int main()
{
    scanf("%lld",&n);
    for(ll i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
    }
    for(ll i=1; i<=n; i++)
    {
        for(ll j=1; j*j<=a[i]; j++)
        {
            if(a[i]%j==0)
            {
                cnt[j]++;
                if(j*j!=a[i])
                {
                    cnt[a[i]/j]++;
                }
            }
        }
    }
    //cout<<"--";
    ll ans=0;
    for(ll i=1000000; i>=2; i--)
    {
        //cout<<"---"<<endl;
        if(cnt[i]==0)continue;
        //cout<<"---"<<endl;
        f[i]=pow_quick(2,cnt[i]-1,mod)*cnt[i]%mod;
        for(ll j=2*i; j<=1000000; j+=i)
        {
            f[i]-=f[j];
            f[i]=(f[i]%mod+mod)%mod;
        }
        ans+=(i*f[i]%mod+mod)%mod;
    }
    printf("%I64d\n",ans%mod);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值