HDU5514Frogs 【容斥定理】

    xiaoxiao2021-03-25  111

    Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2093 Accepted Submission(s): 686 Problem Description 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 Source 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

    明显青蛙i可以跳到位置pos满足: pos%gcd(a[i],m)==0 青蛙i条过的下标总和 可以直接用等差数列求出

    显然 当 gcd(a[i],m)=x gcd(a[j],m)=y lcm(x,y) 的倍数位置都被重复走了2次 到这里已经很明显要容斥了

    然而二进制压缩跑一遍容斥,各种TLE,WA 赛后发现状态数比较多 ,1e9的因子就有100个了 long long的状态会被爆,就算换dfs也会T

    如果预处理出m的所有因子factor,因为gcd(a[i],m)必然是m的因子 定义 used[i]=1 | m的第i个因子的倍数能被青蛙到达 used[i]=0 | 不能被青蛙到达

    ans 就加上 used[i]* factor[i] 如果 used[i]!=0 所有 factor[j]%factor[i]==0 的因子的都不需要再被累计 所以 used[j]-=used[i] | factor[j]

    转载请注明原文地址: https://ju.6miu.com/read-5076.html

    最新回复(0)