ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-07-29 06:03:55  阅读:138  来源: 互联网

标签:stones cnt 变式 Frogs 交不上 stone int xx 权值


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;
}

/*样例区


*/

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

 

标签:stones,cnt,变式,Frogs,交不上,stone,int,xx,权值
来源: https://www.cnblogs.com/er007/p/16530888.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有