1034 Frogs(uvalive可能交不上) 容斥原理变式

There are m stones lying on a circle, and n frogs are jumping over them. The stones are numbered from 0 to m − 1 and the frogs are numbered from 1 to n. The i-th frog can jump over exactly ai stones in a single step, which means from stone j mod m to stone (j + ai) mod m (since all stones lie on a circle). All frogs start their jump at stone 0, 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 20), and the first line contains an integer t, meaning the total number of test cases. For each test case, the first line contains two positive integer n and m - the number of frogs and stones respectively (1 ≤ n ≤ 104 , 1 ≤ m ≤ 109 ). The second line contains n integers a1, a2, · · · , an, where ai denotes step length of the i-th frog (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只青蛙,每只青蛙每步走xi,将所有青蛙可以走到的环形路的位置加起来,问最后的结果是多少。

 分析

 

每个2e9 以内的数最多由10个因子。

把每个数的因子先提取出来,然后在计算公因子之后把它的权值计算进去。

由于有一些公因子是其他公因子的因子,权值会被重复计算,所以要用容斥原理消去这部分的权值。先算出总数再算出不符合条件的数的个数。

正常容斥原理是 - xx + xx * xx - xx * xx * xx + 。。。。

这道题则存了一个cnt数组表示每个因子需要付出的权值。

在消去的时候 for(int j = i + 1; j < v.size();j ++ ) if(v[j] % v[i] == 0 ) cnt[j] -= cnt[i];

在计算权值的时候 * cnt[j] 即可

大佬题解

 

 

//-------------------------代码----------------------------

#define int ll
const int N = 1e5+10;
int n,m;
V<int> v;
ll cnt[N],sum[N];
int kk = 0

void solve()
{
    kk ++ ;
    cin>>n>>m;
    ms(cnt,0);ms(sum,0);
    v.clear();
    for(int i = 2;i * i < m;i ++) {
        if( m % i == 0 ) {
            v.pb(i);v.pb(m/i);
        }
    }
    v.pb(1);
    sort(v.begin(),v.end());
    for(ll i = 1,x,g;i <= n;i++) {
        cin>>x;
        g = gcd(x,m);
        for(int j = 0;j<v.size();j ++ ) {
            if(v[j] % g == 0) 
                cnt[j] = 1;
        }
    }
    ll res = 0;
    for(int i = 0 ; i<v.size();i++) {
        res += m * (m / v[i] - 1) / 2 * (cnt[i] - sum[i]);
        for(int j = i + 1;j<v.size();j ++) {
            if(v[j] % v[i] == 0) sum[j] += cnt[i] - sum[i];
        }
    }
    printf("Case #%lld: %lld\n",kk,res);
}

signed main(){
    clapping();TLE;
    
    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

posted @ 2022-07-29 02:01  er007  阅读(50)  评论(0编辑  收藏  举报