HDU5514Frogs 【容斥定理】

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]%factor[i]==0


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include <string.h>
#include<math.h>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int inf=1e9+7;
const int N = 1e4+5;

int a[N];
vector<int>factor;
int used[500];

int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}

ll f(ll a1,ll an,ll n){
    return (a1+an)*n/2;
}

ll slove(int m){
    ll ans=0;
    for(int i=0;i<factor.size();++i){
        int a=factor[i];
        if(used[i]){
            int num=m/a;
            ans+=used[i]*f(a,a*(num-1),num-1);
            for(int j=i+1;j<factor.size();++j){
                int b=factor[j];
                if(b%a==0){
                    used[j]-=used[i];
                }
            }
        }
    }
    return ans;
}

void init(int m){
    factor.clear();
    for(int i=1,end=sqrt(m+1);i<=end;++i){
        if(m%i==0){
            factor.push_back(i);
            if(i*i!=m){
                factor.push_back(m/i);
            }
        }
    }
    sort(factor.begin(),factor.end());
    fill(used,used+factor.size()+1,0);
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t){
        int n,m;
        scanf("%d%d",&n,&m);
        init(m);
        for(int i=1;i<=n;++i){
            scanf("%d",a+i);
            a[i]=gcd(a[i]%m,m);
            for(int j=0;j<factor.size();++j){
                if(factor[j]%a[i]==0){
                    used[j]=1;
                }
            }
        }
        ll ans=slove(m);
        printf("Case #%d: %lld\n",t,ans);

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值