C. Where is the Pizza?-Educational Codeforces Round 127 (Rated for Div. 2)

Problem - 1671C - Codeforces

C. Dolce Vita

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Turbulent times are coming, so you decided to buy sugar in advance. There are nn shops around that sell sugar: the ii-th shop sells one pack of sugar for aiai coins, but only one pack to one customer each day. So in order to buy several packs, you need to visit several shops.

Another problem is that prices are increasing each day: during the first day the cost is aiai, during the second day cost is ai+1ai+1, during the third day — ai+2ai+2 and so on for each shop ii.

On the contrary, your everyday budget is only xx coins. In other words, each day you go and buy as many packs as possible with total cost not exceeding xx. Note that if you don't spend some amount of coins during a day, you can't use these coins during the next days.

Eventually, the cost for each pack will exceed xx, and you won't be able to buy even a single pack. So, how many packs will you be able to buy till that moment in total?

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Next tt cases follow.

The first line of each test case contains two integers nn and xx (1≤n≤2⋅1051≤n≤2⋅105; 1≤x≤1091≤x≤109) — the number of shops and your everyday budget.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial cost of one pack in each shop.

It's guaranteed that the total sum of nn doesn't exceed 2⋅1052⋅105.

Output

For each test case, print one integer — the total number of packs you will be able to buy until prices exceed your everyday budget.

Example

input

Copy

4
3 7
2 1 2
5 9
10 20 30 40 50
1 1
1
2 1000
1 1

output

Copy

11
0
1
1500

Note

In the first test case,

  • Day 1: prices are [2,1,2][2,1,2]. You can buy all 33 packs, since 2+1+2≤72+1+2≤7.
  • Day 2: prices are [3,2,3][3,2,3]. You can't buy all 33 packs, since 3+2+3>73+2+3>7, so you buy only 22 packs.
  • Day 3: prices are [4,3,4][4,3,4]. You can buy 22 packs with prices 44 and 33.
  • Day 4: prices are [5,4,5][5,4,5]. You can't buy 22 packs anymore, so you buy only 11 pack.
  • Day 5: prices are [6,5,6][6,5,6]. You can buy 11 pack.
  • Day 6: prices are [7,6,7][7,6,7]. You can buy 11 pack.
  • Day 7: prices are [8,7,8][8,7,8]. You still can buy 11 pack of cost 77.
  • Day 8: prices are [9,8,9][9,8,9]. Prices are too high, so you can't buy anything.

In total, you bought 3+2+2+1+1+1+1=113+2+2+1+1+1+1=11 packs.

In the second test case, prices are too high even at the first day, so you can't buy anything.

In the third test case, you can buy only one pack at day one.

In the fourth test case, you can buy 22 packs first 500500 days. At day 501501 prices are [501,501][501,501], so you can buy only 11 pack the next 500500 days. At day 10011001 prices are [1001,1001][1001,1001] so can't buy anymore. In total, you bought 500⋅2+500⋅1=1500500⋅2+500⋅1=1500 packs.

典型的并查集

7
1 2 3 4 5 6 7
2 3 1 7 6 5 4
2 0 1 0 0 0 0

第一个样例为例,那1为例,1要么在第一个位置,要么在第3个位置,我们对相冲突的数字进行集合合并的并查集处理,会发现,这一集合一旦一个数字固定,其他数字只有一种选法

例如 1 2 3 

        2 3 1

1在第一个位置时,2只能在第二个位置,3只能在第三个位置1,同理1在第三个位置也是如此。

但我们还要特判一下特殊集合

1

1

这种情况,也就代表了这一集合只有一种方案

还有就是c数组的处理,一旦c数组固定住了某个数字,就代表这个数字所在的集合,即其父亲,方案数永久变成了1

由于方案数比较多,我们跑一下快速幂

# include<iostream>
# include<vector>
# include<cstring>
# include<map>
# include<algorithm>
# define mod 1000000007
using namespace std;
typedef long long int ll;

int f[500000+10];
int a[500000+10],b[500000+10],c[500000+10];

int getf(int x)
{
    if(x==f[x])
        return f[x];
    else
    {
        f[x]=getf(f[x]);

        return f[x];

    }
}

int hebing(int x,int y)
{
    int t1=getf(x);
    int t2=getf(y);

    if(t1!=t2)
    {
        f[t2]=t1;

    }
}


ll qp(ll base,ll pow,ll mo)
{
    ll ans=1;

    while(pow)
    {
        if(pow&1)
        {
            ans=ans*base%mo;

        }
        base=base*base%mo;

        pow>>=1;

    }

    return ans;

}
int main ()
{

  int t;

  cin>>t;

  while(t--)
  {
      int n;

      cin>>n;

      for(int i=1;i<=n;i++)
      {
          cin>>a[i];

          f[i]=i;

      }

      for(int i=1;i<=n;i++)
      {
          cin>>b[i];

          if(a[i]==b[i])
            {
                f[a[i]]=0;
                continue;
            }

          hebing(a[i],b[i]);

      }

      for(int i=1;i<=n;i++)
      {
          cin>>c[i];

          int ff=getf(c[i]);

          f[ff]=0;

      }

      ll cnt=0;

      for(int i=1;i<=n;i++)
      {
          int ff=getf(i);

          if(ff==i)
          {
              cnt++;

          }
      }

      cout<<qp(ll(2),cnt,mod)<<endl;

  }


    return 0;

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值