Skip to content

泉州一中信息学Blog

信息学奥赛技术分享博客

  • 登录
  • 小学oj
  • 中学oj
  • 测试页面1
  • Toggle search form

10.20 一点考前复习-2

Posted on 2023年10月20日2023年10月20日 By 张, 高畅 10.20 一点考前复习-2无评论

中国剩余定理/GAUSS消元

中国剩余定理:
设x\in Z
若有:
x \equiv a_1 \pmod{m_1}
x \equiv a_2 \pmod{m_2}
x \equiv a_3 \pmod{m_3}
…
x \equiv a_n \pmod{m_n}

令M=m_1\cdot m_2 \cdot m_3\cdot …\cdot m_n
M_i=\tfrac{M}{m_i}
$M_it_i \equiv 1\pmod{mi}会有x=\sum{i=1}^n a_iM_it_i$
模板题

#include
using namespace std;
const int N=15;
typedef long long LL;
int n,A[N],B[N];
LL M=1,res=0;
void exgcd(LL a,LL b,LL &x,LL &y)
{
    if(!b)
    {
        x=1;
        y=0;
    }
    else
    {
        exgcd(b,a%b,y,x);
        y-=a/b*x;
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&A[i],&B[i]);
        M*=(LL)A[i];
    }
    for(int i=0;i<n;i++)
    {
        LL mi=M/A[i],t,x;
        exgcd(mi,A[i],t,x);
        res+=B[i]*mi*t;
    }
    printf("%lld",(res%M+M)%M);
    return 0;
}

高斯消元
板子

#include
#include
#include
#include
#include
using namespace std;
const int N=110;
const double eps=1e-6;
int n;
double a[N][N];
int gauss()
{
    int c,r;
    for(c=0,r=0;c<n;c++)
    {
        int t=r;
        for(int i=r;ifabs(a[t][c]))
                t=i;
        if(fabs(a[t][c])<eps)continue;
        for(int i=c;i=c;i--)a[r][i]/=a[r][c];
        for(int i=r+1;ieps)
                for(int j=n;j>=c;j--)
                    a[i][j]-=a[r][j]*a[i][c];
        r++;
    }
    if(r<n)
    {
        for(int i=r;ieps)
                return 2;
        return 1;
    }
    for(int i=n-1;i>=0;i--)
        for(int j=i+1;j<n;j++)
            a[i][n]-=a[i][j]*a[j][n];
    return 0;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        for(int j=0;j<=n;j++)
            scanf("%lf",&a[i][j]);
    int t=gauss();
    if(!t)
    {
        for(int i=0;i<n;i++)printf("%.2lf\n",a[i][n]);
    }
    else if(t==1)printf("Infinite group solutions");
    else printf("No solution");
    return 0;
}

关于 Tarjan

首先是这位割点

#include
using namespace std;
const int N=2e4+5;
int n,m,root;
int flag[N],dfn[N],low[N],timestamp;
vectorG[N];
void tarjan(int u)
{
    int cnt=0;
    dfn[u]=low[u]=++timestamp;
    for(int j:G[u])
    {
        if(!dfn[j])
        {
            tarjan(j);
            low[u]=min(low[u],low[j]);
            if(u==root)cnt++;
            if(u!=root&&dfn[u]=2)flag[u]=1;
}
int main()
{
    scanf("%d%d",&n,&m);
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
        {
            root=i;
            tarjan(i);
        }
    }
    int res=0;
    for(int i=1;i<=n;i++)res+=flag[i];
    printf("%d\n",res);
    for(int i=1;i<=n;i++)
    {
        if(flag[i])printf("%d ",i);
    }
    return 0;
}

强联通分量(SCC)

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=3e5+5;
int n,m;
int h[N],e[M],ne[M],idx;
int dfn[N],low[N],timestamp;
int stk[N],top,in_stk[N];
int id[N],scc_cnt,Size[N];
int out[N],in[N];
void add(int a,int b){e[idx]=b,ne[idx]=h[a],h[a]=idx++;}
void tarjan(int u)
{
    dfn[u]=low[u]=++timestamp;
    stk[++top]=u;
    in_stk[u]=1;
    for(int i=h[u];i!=-1;i=ne[i])
    {
        int j=e[i];
        if(!dfn[j])
        {
            tarjan(j);
            low[u]=min(low[u],low[j]);
        }
        else if(in_stk[j])low[u]=min(low[u],dfn[j]);
    }
    if(dfn[u]==low[u])
    {
        ++scc_cnt;
        int y;
        do
        {
            y=stk[top--];
            in_stk[y]=0;
            id[y]=scc_cnt;
            Size[scc_cnt]++;
        }
        while(y!=u);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(h,-1,sizeof h);
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])tarjan(i);
    }
    int res=0;
    for(int i=1;i<=scc_cnt;i++)res=max(res,Size[i]);
    printf("%d\n",res);
    if(scc_cnt==1)puts("0");
    else
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=h[i];~j;j=ne[j])
            {
                int k=e[j];
                if(id[i]!=id[k])
                {
                    out[id[i]]++; 
                    in[id[k]]++;
                }
            }
        }
        int none_out_cnt=0,none_in_cnt=0;
        for(int i=1;i<=scc_cnt;i++)
        {
            if(!in[i])none_in_cnt++;
            if(!out[i])none_out_cnt++;
        }
        printf("%d",max(none_in_cnt,none_out_cnt));
    }
    return 0;
}

边双联通分量(E-DCC)

#include
using namespace std;
const int N=5e5+5,M=4e6+5;
int n,m,h[N],e[M],ne[M],idx,dfn[N],low[N],timestamp,stk[N],top,d[N];
vector<vector >res;
void add(int a,int b){e[idx]=b,ne[idx]=h[a],h[a]=idx++;}
void tarjan(int u,int from)
{
    dfn[u]=low[u]=++timestamp;
    stk[++top]=u;
    for(int i=h[u];~i;i=ne[i])
    {
        int j=e[i];
        if(!dfn[j])
        {
            tarjan(j,i);
            low[u]=min(low[u],low[j]);
        }
        else if(i!=(from^1))low[u]=min(low[u],dfn[j]);
    }
    if(dfn[u]==low[u])
    {
        vectortmp;
        int y;
        do 
        {
            y=stk[top--];
            tmp.push_back(y);
        }
        while(y!=u);
        res.push_back(tmp);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(h,-1,sizeof h);
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])tarjan(i,-1);
    }
    printf("%d\n",res.size());
    for(auto item:res)
    {
        printf("%d ",item.size());
        for(auto j:item)printf("%d ",j);
        puts("");   
    }
    return 0;
}

点双联通分量(V-DCC)

#include
using namespace std;
const int N=5e5+5,M=4e6+5;
int n,m;
int h[N],e[M],ne[M],idx;
int dfn[N],low[N],timestamp;
int stk[N],top;
int cnt;
vectordcc[N];
void add(int a,int b){e[idx]=b,ne[idx]=h[a],h[a]=idx++;}
void tarjan(int u,int father)
{
    dfn[u]=low[u]=++timestamp;
    stk[++top]=u;
    int son=0;
    for(int i=h[u];~i;i=ne[i])
    {
        int j=e[i];
        if(!dfn[j])
        {
            son++;
            tarjan(j,u);
            low[u]=min(low[u],low[j]);
            if(dfn[u]<=low[j])
            {
                cnt++;
                int y;
                do 
                {
                    dcc[cnt].push_back(y=stk[top--]);
                }
                while(y!=j);
                dcc[cnt].push_back(u);
            }
        }
        else if(j!=father)low[u]=min(low[u],dfn[j]);
    }
    if(!father&&!son)dcc[++cnt].push_back(u);
}
int main()
{
    memset(h,-1,sizeof h);
    scanf("%d%d",&n,&m);
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    for(int root=1;root<=n;root++)
    {
        if(!dfn[root])
        {
            top=0;
            tarjan(root,0);
        }
    }
    printf("%d\n",cnt);
    for(int i=1;i<=cnt;i++)
    {
        printf("%d ",dcc[i].size());
        for(auto j:dcc[i])printf("%d ",j);
        puts("");
    }
    return 0;
}
训练日志

文章导航

Previous Post: 10.20 一点考前复习-1
Next Post: 赛前集训DAY3

发表回复 取消回复

要发表评论,您必须先登录。

2025年 6月
一 二 三 四 五 六 日
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« 2月    

2024常州 Class Classic OI Problems Contest cqr的长乐集训2023 CZYZ LOC New Game NOI NOIP Password Protected PM_PK Preview Problems Retrospect Selfmade Qusetion STL The end Training Uneasy Problem 蒟蒻 通报

  • 训练日志
  • 链表
  • 入门
  • 模拟
  • dfs序
  • 并查集
  • spfa
  • 最小割
  • 矩阵树定理
  • 仙人掌
  • BSGS
  • 凸包
  • 回文自动机
  • 递推与动归
  • 堆
  • 莫队算法
  • ST表
  • Treap
  • 树套树
  • 可持久化线段树
  • 初赛
  • 搜索
  • 贪心
  • 深度优先搜索
  • 欧拉图
  • dijkstra
  • 费用流
  • 哈夫曼树
  • kruskual
  • 置换
  • 旋转卡壳
  • KMP
  • 区间动归
  • STL
  • 链表
  • 可并堆
  • sply
  • 主席树
  • 可持久化字典树
  • 算法
  • 动态规划
  • 构造
  • 广度优先搜索
  • 最短路
  • floyd
  • 最大流
  • 虚树
  • prim
  • 筛法
  • 半平面交
  • 字典树
  • 背包动归
  • 基础数据结构
  • 分块
  • 线段树
  • 替罪羊树
  • K-DTree
  • 图论
  • 二分法
  • 迭代搜索
  • 拓扑排序
  • 有上下界网络流
  • 生成树
  • 快速幂
  • 后缀数组
  • 树形动归
  • 哈希表
  • 中级数据结构
  • 平衡树
  • 可持久化数据结构
  • 数据结构
  • 三分法
  • 启发式搜索
  • 图的连通
  • 点分治
  • 博弈论
  • AC自动机
  • 状压动归
  • 单调栈
  • 树状数组
  • 高级数据结构
  • OI资料
  • 数学
  • 高精度
  • 差分约束
  • 树上倍增
  • 素数测试
  • 后缀自动机
  • 数位动归
  • 单调队列
  • 新闻
  • 几何
  • 随机化
  • 二分图染色
  • 树链剖分
  • 欧拉函数
  • manacher
  • 斜率优化
  • 离线处理
  • 信息学奥赛学长风采
  • 字符串
  • 二分图匹配
  • prufer编码
  • 卡特兰数
  • 密码学
  • 决策单调
  • 赛后总结
  • 其他
  • 2-SAT
  • 最近公共祖先
  • 矩阵乘法
  • 记忆化搜索
  • 网络流
  • Link cut tree
  • 排列组合
  • 树
  • 高斯消元
  • 乘法逆元
  • 容斥原理
  • 调和级数
  • 概率与期望
  • 模线性方程组
  • 莫比乌斯反演
  • 快速傅里叶变换
  • 扩展欧几里德
  • 最大公约数与最小公倍数

近期文章

  • DP杂题
  • 2025年2月13日模拟赛
  • HLOJ-TEST ROUND 4-T1/T2(构造)- 3
  • HLOJ-TEST ROUND 4-T1/T2(构造)- 2
  • HLOJ-TEST ROUND 4-T1/T2(构造)- 1

近期评论

归档

  • 2025年2月
  • 2025年1月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月

Copyright © 2025 泉州一中信息学Blog.

Powered by PressBook WordPress theme