Long ago, you thought of two finite arithmetic progressions AA and BB. Then you found out another sequence CC containing all elements common to both AA and BB. It is not hard to see that CC is also a finite arithmetic progression. After many years, you forgot what AA was but remember BB and CC. You are, for some reason, determined to find this lost arithmetic progression. Before you begin this eternal search, you want to know how many different finite arithmetic progressions exist which can be your lost progression AA.
Two arithmetic progressions are considered different if they differ in their first term, common difference or number of terms.
It may be possible that there are infinitely many such progressions, in which case you won't even try to look for them! Print −1−1 in all such cases.
Even if there are finite number of them, the answer might be very large. So, you are only interested to find the answer modulo 109+7109+7.
Input
The first line of input contains a single integer tt (1≤t≤1001≤t≤100) denoting the number of testcases.
The first line of each testcase contains three integers bb, qq and yy (−109≤b≤109−109≤b≤109, 1≤q≤1091≤q≤109, 2≤y≤1092≤y≤109) denoting the first term, common difference and number of terms of BB respectively.
The second line of each testcase contains three integers cc, rr and zz (−109≤c≤109−109≤c≤109, 1≤r≤1091≤r≤109, 2≤z≤1092≤z≤109) denoting the first term, common difference and number of terms of CC respectively.
Output
For each testcase, print a single line containing a single integer.
If there are infinitely many finite arithmetic progressions which could be your lost progression AA, print −1−1.
Otherwise, print the number of finite arithmetic progressions which could be your lost progression AA modulo 109+7109+7. In particular, if there are no such finite arithmetic progressions, print 00.
Example
input
Copy
8 -3 1 7 -1 2 4 -9 3 11 0 6 3 2 5 5 7 5 4 2 2 11 10 5 3 0 2 9 2 4 3 -11 4 12 1 12 2 -27 4 7 -17 8 2 -8400 420 1000000000 0 4620 10
output
Copy
0 10 -1 0 -1 21 0 273000
Note
For the first testcase, B={−3,−2,−1,0,1,2,3}B={−3,−2,−1,0,1,2,3} and C={−1,1,3,5}C={−1,1,3,5}. There is no such arithmetic progression which can be equal to AA because 55 is not present in BB and for any AA, 55 should not be present in CC also.
For the second testcase, B={−9,−6,−3,0,3,6,9,12,15,18,21}B={−9,−6,−3,0,3,6,9,12,15,18,21} and C={0,6,12}C={0,6,12}. There are 1010 possible arithmetic progressions which can be AA:
- {0,6,12}{0,6,12}
- {0,2,4,6,8,10,12}{0,2,4,6,8,10,12}
- {0,2,4,6,8,10,12,14}{0,2,4,6,8,10,12,14}
- {0,2,4,6,8,10,12,14,16}{0,2,4,6,8,10,12,14,16}
- {−2,0,2,4,6,8,10,12}{−2,0,2,4,6,8,10,12}
- {−2,0,2,4,6,8,10,12,14}{−2,0,2,4,6,8,10,12,14}
- {−2,0,2,4,6,8,10,12,14,16}{−2,0,2,4,6,8,10,12,14,16}
- {−4,−2,0,2,4,6,8,10,12}{−4,−2,0,2,4,6,8,10,12}
- {−4,−2,0,2,4,6,8,10,12,14}{−4,−2,0,2,4,6,8,10,12,14}
- {−4,−2,0,2,4,6,8,10,12,14,16}{−4,−2,0,2,4,6,8,10,12,14,16}
For the third testcase, B={2,7,12,17,22}B={2,7,12,17,22} and C={7,12,17,22}C={7,12,17,22}. There are infinitely many arithmetic progressions which can be AA like:
- {7,12,17,22}{7,12,17,22}
- {7,12,17,22,27}{7,12,17,22,27}
- {7,12,17,22,27,32}{7,12,17,22,27,32}
- {7,12,17,22,27,32,37}{7,12,17,22,27,32,37}
- {7,12,17,22,27,32,37,42}{7,12,17,22,27,32,37,42}
- …
思路:这个题是求公差的最小公倍数,因为C中的数的差要大于B才行,那么C中的数才能“包含”B中的数,那么C的公差要比B的公差大多少才能将他们都包含呢?那一定也是符合B的公差的,那么就是B的倍数(看到倍数后,这时其实就要注意到有可能是要求最小公倍数的),刚好C也要包含A,那么C的公差就是A与B的公差的积了。这里为什么要是最小公倍数呢?如果C的公差大于A,B的积,那么重合的数是不是在C中没有算上,那C“最小”该是多少呢?就是A,B的最小公倍数,同时满足AB条件即可的最小值那么就是最小公倍数,(C如果在小的话,C出现的在另两个中没有出现,C的公差是最小公倍数就刚好会不多不少)。只需要特判输出0和-1的情况即可。最后是怎么算扩展的位数呢?向右能扩展多少呢,向右顶到头只能有C公差/A公差个,左端也是同理,因为左右是组合的,所以要左边个数乘右边个数。我们枚举C的因数就是在枚举A的公差,只要枚举的这个数符合A*B的最小公倍数是C即可。(特判情况:在B与C公差相等的情况下,C中的下一位数超过了B,那么就会能够无线扩展。构造不出来A的情况:首先就是公差不相同,然后就是C的首项与B的首项差不是B的公差,这是说明C没有在B中出现,,或者是C的首项直接就超过了B的末项,然后就是,C的末项要包含在B中,不能超过B)
完整代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
void solve()
{
int bs,bq,bsum,cs,cq,csum;
cin>>bs>>bq>>bsum>>cs>>cq>>csum;
if(cq%bq||cs<bs||(cs-bs)%bq||cs+(csum-1)*cq>bs+(bsum-1)*bq)
{
cout<<"0"<<endl;
return;
}
if(cs+csum*cq>bs+(bsum-1)*bq||cs-cq<bs)
{
cout<<"-1"<<endl;
return;
}
int ans=0;
for(int i=1;i*i<=cq;i++)
{
if(cq%i==0)
{
if(i*bq/__gcd(i,bq)==cq)
{
ans=(ans+cq/i*cq/i)%mod;
}
if(i*i!=cq)
{
int x=cq/i;
if(x*bq/__gcd(x,bq)==cq)
{
ans=(ans+cq/x*cq/x)%mod;
}
}
}
}
cout<<ans<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}