【容斥好题】HDU - 5514 F - Frogs

F - Frogs  HDU - 5514

There are mm stones lying on a circle, and nn frogs are jumping over them. 
The stones are numbered from 00 to m−1m−1 and the frogs are numbered from 11 to nn. The ii-th frog can jump over exactly aiai stones in a single step, which means from stone j mod mj mod m to stone (j+ai) mod m(j+ai) mod m (since all stones lie on a circle). 

All frogs start their jump at stone 00, then each of them can jump as many steps as he wants. A frog will occupy a stone when he reach it, and he will keep jumping to occupy as much stones as possible. A stone is still considered ``occupied" after a frog jumped away. 
They would like to know which stones can be occupied by at least one of them. Since there may be too many stones, the frogs only want to know the sum of those stones' identifiers.

Input

There are multiple test cases (no more than 2020), and the first line contains an integer tt, 
meaning the total number of test cases. 

For each test case, the first line contains two positive integer nn and mm - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109)(1≤n≤104, 1≤m≤109). 

The second line contains nn integers a1,a2,⋯,ana1,a2,⋯,an, where aiai denotes step length of the ii-th frog (1≤ai≤109)(1≤ai≤109).

Output

For each test case, you should print first the identifier of the test case and then the sum of all occupied stones' identifiers.

Sample Input

3
2 12
9 10
3 60
22 33 66
9 96
81 40 48 32 64 16 96 42 72

Sample Output

Case #1: 42
Case #2: 1170
Case #3: 1872

题意:有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过xi个石子。

           问所有被至少踩过一次的石子的序号之和。

#include 
#define ll long long
using namespace std;
const int maxn=1e5+10;
ll fac[maxn],vis[maxn],num[maxn];

int main()
{
    int T,cas=0;
    scanf("%d",&T);
    while(T--)
    {
        ll ans=0,m,x;
        int n,tot=0;
        scanf("%d%lld",&n,&m);
        memset(vis,0,sizeof(vis)); //m的因数%a[i]==0
        memset(num,0,sizeof(num));
        for(ll i=1;i*i<=m;i++)
        {
            if(m%i==0)
            {
                fac[++tot]=i;
                if(i*i!=m)
                    fac[++tot]=m/i;
            }
        }
        sort(fac+1,fac+tot+1);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&x);
            ll p=__gcd(x,m);
            for(int j=1;j

 

你可能感兴趣的:(容斥)