各种模板(待补全)

昨天DQS来给我们开班会辣,DQS曰:“板子很重要!”。所以就来搞板子…
听说板子要加图?
这里写图片描述
SPFA(1557热浪)

#include<cstdio>
#include<queue>
using namespace std;
const int maxn=50000;
struct Edge
{
    int ff;
    int to;
    int d;
    int next;
}edge[maxn];
int head[maxn];
bool vis[maxn];
int dist[maxn];
int tot;
void add(int fff,int tt,int d)
{
    edge[++tot].ff=fff;
    edge[tot].to=tt;
    edge[tot].d=d;
    edge[tot].next=head[fff];
    head[fff]=tot;
}
queue<int >q;
void spfa(int s)
{
    dist[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int x=q.front();
        vis[x]=0;
        q.pop();
        for(int i=head[x];i;i=edge[i].next)
        {
            Edge e=edge[i];
            if(dist[e.to]>dist[x]+e.d)
            {
                dist[e.to]=dist[x]+e.d;
                if(!vis[e.to])
                {
                    q.push(e.to);
                    vis[e.to]=1;
                }
            }
        }
    }
}
int main()
{
    int n,m,st,se;
    scanf("%d%d%d%d",&n,&m,&st,&se);
    for(int i=1;i<=n;i++)
    dist[i]=0x7fffffff;
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    spfa(st);
    printf("%d",dist[se]);
    return 0;
}

这里写图片描述
dijsktra(热浪

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=100000*2;
const int maxm=1500;
const int inf=0x7fffffff;

struct heap
{
    int p;
    int dis;
    bool operator < (const heap &hah)const
    {
        return dis > hah.dis;
    } 
};
int n,m,st,se;
priority_queue<heap>q;
struct Edge
{
    int to;
    int d;
    int next;
}edge[maxn];

bool vis[maxn];
int head[maxn];
int dist[maxn];
int tot;
void add(int f,int t,int d)
{
    edge[++tot].to=t;
    edge[tot].d=d;
    edge[tot].next=head[f];
    head[f]=tot;
} 
bool flag=false;
void DJ() 
{
    for(int i=1;i<=n;i++)
        dist[i]=0x7fffffff;
    memset(vis,0,sizeof(vis));
    q.push((heap){st,0});
    int cnt=0;
    while(!q.empty())
    {
        heap p=q.top();
        q.pop();
        if(p.p==se)
        {
            printf("%d",p.dis);
            return;
        }
        if(vis[p.p])
            continue;
        vis[p.p] = true;
        for(int i=head[p.p];i;i=edge[i].next)
        {
            Edge e=i[edge];
            int v=edge[i].to;
            if(dist[v]>dist[p.p]+e.d)
            {
                dist[v]=dist[p.p]+e.d;
                q.push((heap){v,p.dis+edge[i].d});
            }
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&st,&se);
    for(int i=1;i<=m;i++)
    {
        int a,b;
        int c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    DJ();
    return 0;
}

这里写图片描述
kruskal(裸

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
const int maxm=200000+500;
const int inf=0x7fffffff;
int n,m;
struct meico
{
    int f;
    int to;
    int d;
}sz[maxm];

int head[maxm],dist[maxm];
int tott;
void add2(int f,int t,int d)
{
    sz[++tott].f=f;
    sz[tott].to=t;
    sz[tott].d=d;
}
int fa[maxm];
int find(int x)
{
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
void kclr()
{
    for(int i=1;i<=n;i++)
        fa[i]=i;
}
bool cmp(meico a,meico b)
{
    return a.d<b.d;
}
void K()
{
    kclr();
    int ans=0;
    sort(sz+1,sz+tott+1,cmp);
    for(int i=1;i<=tott;i++)
    {
        int ff=find(sz[i].f);
        int tt=find(sz[i].to);
        if(ff!=tt)
        {
            fa[ff]=tt;
            ans+=sz[i].d;
        }
    }
    printf("%d\n",ans);
}
void read(int &a)
{
    a=0;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        c=getchar();
    }
    int ans=0;
    while(c<='9'&&c>='0')
    {
        ans=(ans<<1)+(ans<<3);
        ans+=c-'0';
        c=getchar();
    }
    a=ans;
    return;
} 
int main()
{
    read(n),read(m);
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        read(a),read(b),read(c);
        add2(a,b,c);
        add2(b,a,c);
    }
    K();    
    return 0;
}

这里写图片描述
倍增LCA(小机房的树

#include<algorithm>
#include<cstdio> 
using namespace std;
const int maxn=150050;
int n,m;
int fa[maxn][30]; 
int dist[maxn];
int deep[maxn];
struct Edge
{
    int to;
    int d;
    int next;
}edge[maxn];
int head[maxn];
int tot;
void add(int f,int t,int d)
{
    ++tot,tot[edge]=(Edge){t,d,f[head]};
    f[head]=tot; 
}
void dfs(int t,int f,int dep,int dis)
{
    0[t[fa]]=f,t[deep]=dep,dist[t]=dist[f]+dis;
    for(int i=t[head];i;i=edge[i].next)
    {
        Edge e=i[edge];
        if(e.to[deep])
            continue;
        dfs(e.to,t,dep+1,e.d);
    }
}
int ii;
void clr()
{
    for(ii=1;(1<<ii)<=n;ii++);//ii = log2(n) + 1;
    for(int j=1;j<=ii;j++)
    for(int i=0;i<n;i++)
        fa[i][j]=fa[fa[i][j-1]][j-1];

}
int lca(int x,int y)
{
    int a=x,b=y;
    int ans=0;
    if(deep[x]<deep[y])
        swap(x,y);
    for(int j=ii;j>=0;j--)
        if(deep[fa[x][j]]>=deep[y])
            x=fa[x][j];

    if(x==y)
        return dist[a]+dist[b]-(dist[x]<<1);

    for(int j=ii;j>=0;j--)
        if(fa[x][j]!=fa[y][j])
            x=fa[x][j],y=fa[y][j];

    x=fa[x][0];
    return dist[a]+dist[b]-(dist[x]<<1);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c),add(b,a,c);
    }

    dfs(0,50005,1,0);
    clr();

    int q;
    scanf("%d",&q);
    while(q--)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        printf("%d\n",lca(a,b));
    }
    return 0;
}

这里写图片描述
tarjan求SCC(信息传递

#include<cstdio>
#include<stack>
#include<iostream>

using namespace std;
const int maxn=500000+10;
int head[maxn];
int scc[maxn];
int pre[maxn];
int low[maxn];

struct Edge
{
    int f;
    int to;
    int next;
}edge[maxn];
int tot;
void add(int fff,int ttt)
{
    edge[++tot].f=fff;
    edge[tot].to=ttt;
    edge[tot].next=head[fff];
    head[fff]=tot;
}

stack<int> s;
int ans=0x7fffffff;
int scccnt;
int dfs_clock;

void dfs(int u)
{
    pre[u]=low[u]=++dfs_clock;
    s.push(u);
    for(int i=head[u];i;i=edge[i].next)
    {
        Edge e=edge[i];
        if(!pre[e.to])
        {
            dfs(e.to);
            low[u]=min(low[u],low[e.to]);
        }
        if(!scc[e.to])
        {
            low[u]=min(low[u],pre[e.to]);
        }
    }
    if(pre[u]==low[u])
    {
        int sccsz=0;
        scccnt++;
        while(1)
        {
            int x=s.top();
            scc[x]=scccnt;
            s.pop();
            sccsz++;
            if(x==u)
            {
                if(sccsz!=1)
                ans=min(ans,sccsz);
                break;
            }       
        }
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        add(i,x);
    }
    for(int i=1;i<=n;i++)
    {
        if(!pre[i])
        dfs(i);
    }
    printf("%d",ans);
    return 0;
}

这里写图片描述
tarjan缩点(爱在心中

#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=100000+500;
const int maxm=400000+500;
int n,m;
int ff[maxn],tt[maxn];
struct Edge
{
    int f;
    int to;
    int next;
}edge[maxm];
int tot;
int head[maxn]; 
void add(int f,int t)
{
    edge[++tot].f=f;
    edge[tot].to=t;
    edge[tot].next=head[f];
    head[f]=tot;
}
int pre[maxn],low[maxn];
int scc[maxn];
int cd[maxn],rd[maxn];
int dfs_cnt;
stack<int>s;
int scnt;
int hcnt;
bool bigscc[maxn];
void dfs(int x)
{
    pre[x]=low[x]=++dfs_cnt;
    s.push(x);
    for(int i=head[x];i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!pre[v])
        {
            dfs(v);
            low[x]=min(low[x],low[v]);
        }
        if(!scc[v])
        {
            low[x]=min(low[x],pre[v]);
        }
    }
    if(pre[x]==low[x])
    {
        int nn=0;
        scnt++;
        while(1)
        {
            nn++;
            int hah=s.top();
            scc[hah]=scnt;
            s.pop();
            if(hah==x)
            {
                if(nn>=2)
                {
                    bigscc[scnt]=1;
                    hcnt++;
                }
                break;
            }
        }
    }
}
int sccn;
bool pd()
{
    int cdcnt=0;
    for(int i=1;i<=scnt;i++)
    {
        if(cd[i]==0)
        {
            cdcnt++;
            sccn=i;
        }
    }
    if(cdcnt==1&&bigscc[sccn])
    return true;
    else
    return false;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&ff[i],&tt[i]);
        add(ff[i],tt[i]);
    }
    for(int i=1;i<=n;i++)
    {
        if(!pre[i])
        dfs(i);
    }
    printf("%d\n",hcnt);
    for(int i=1;i<=m;i++)
    {
        if(scc[ff[i]]!=scc[tt[i]])
        {
            cd[scc[ff[i]]]++;
            rd[scc[tt[i]]]++;
        }
    }
    if(!pd())
    {
        puts("-1");
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        if(scc[i]==sccn)
        printf("%d ",i);
    }
    return 0;
}

这里写图片描述
ST表(POJ3264

#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=50000+500;
int num[maxn];
int st[maxn][30];
int st1[maxn][30];
int n;
void ST()
{
    for(int i=1;i<=n;i++)
        st[i][0]=st1[i][0]=num[i];
    int k=(log(n)/log(2));
    for(int j=1;j<=k;j++)
    {
        for(int i=1;i<=n;i++)
        {
            if(i+(1<<(j-1))<=n)
            {
                st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
                st1[i][j]=min(st1[i][j-1],st1[i+(1<<(j-1))][j-1]);
            }
        }
    }
}
int ask_max(int x,int y)
{
    int k=log(y-x+1)/log(2);
    return max(st[x][k],st[y-(1<<k)+1][k]);
}
int ask_min(int x,int y)
{
    int k=log(y-x+1)/log(2);
    return min(st1[x][k],st1[y-(1<<k)+1][k]);
}
int main()
{
    int q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    ST(); 
    while(q--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",ask_max(a,b)-ask_min(a,b)); 
    }
    return 0;
}

线段树(CODEVS 线段树练习3

#include<cstdio>
using namespace std;
const int maxn=2500000;
typedef long long ll;
struct tree
{
    ll l;
    ll r;
    ll sum;
    ll add;
}t[maxn<<2];
ll num[maxn];
void up(ll p)
{
    t[p].sum=(t[p<<1].sum+t[p<<1|1].sum);
}
void buff(ll p)
{
    if(t[p].add)
    {
        t[p<<1].sum+=t[p].add*(t[p<<1].r-t[p<<1].l+1);
        t[p<<1|1].sum+=t[p].add*(t[p<<1|1].r-t[p<<1|1].l+1);
        t[p<<1].add+=t[p].add;
        t[p<<1|1].add+=t[p].add;
        t[p].add=0;
    }
    up(p);
}
void expand(ll l,ll r,ll p)
{
    t[p].r=r;
    t[p].l=l;
    if(r==l)
    {
        t[p].sum=num[l];
        return;
    }
    ll mid=(l+r)>>1;
    expand(l,mid,p<<1);
    expand(mid+1,r,p<<1|1);
    up(p);
}
void addd(ll l,ll r,ll p,ll v)
{
    if(l<=t[p].l&&t[p].r<=r)
    {
        t[p].sum+=(t[p].r-t[p].l+1)*v;
        t[p].add+=v;
        return;
    }
    buff(p);
    up(p);
    ll mid=(t[p].l+t[p].r)>>1;
    if(l<=mid)
        addd(l,r,p<<1,v);
    if(mid+1<=r)
        addd(l,r,p<<1|1,v);
    buff(p);
    up(p);
}
ll ask(ll l,ll r,ll p)
{
    ll ans=0;
    if(l<=t[p].l&&t[p].r<=r)
    {
        return t[p].sum;
    }
    buff(p);
    up(p);
    ll mid=(t[p].l+t[p].r)>>1;
    if(l<=mid)
        ans+=ask(l,r,p<<1);
    if(mid+1<=r)
        ans+=ask(l,r,p<<1|1);
    return ans;
}
int main()
{
    ll n,q;
    scanf("%lld",&n);
    scanf("%lld",&q);
    expand(1,n,1);
    while(q--)
    {
        ll hah;
        scanf("%lld",&hah);
        if(hah==1)
        {
            ll a,b;
            ll c;
            scanf("%lld%lld%lld",&a,&b,&c);
            addd(a,b,1,c);
        }
        else
        {
            ll a,b;
            scanf("%lld%lld",&a,&b);
            printf("%lld",ask(a,b,1));
        }
    }
    return 0;
}

这里写图片描述
//禁奥义·树链剖分(边)
/*

#include<algorithm>
#include<iostream>
#include<cstring> 
#include<string>
#include<cstdio>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=100000+500;
struct Edge
{
    int to;
    int d;
    int next;
}edge[maxn];

int tot;
int n;
int head[maxn];
int add(int f,int t,int d)
{
    edge[++tot]=(Edge){t,d,head[f]};
    head[f]=tot;
}

int ff[maxn],tt[maxn],dd[maxn];

int siz[maxn],deep[maxn],top[maxn];
int fa[maxn],son[maxn];
void dfs1(int f,int t)
{
    deep[t]=deep[f]+1;
    fa[t]=f;
    siz[t]=1; 
    for(int i=head[t];i;i=edge[i].next)
    {
        Edge e=edge[i];
        if(e.to==f)
        continue;
        dfs1(t,e.to);
        siz[t]+=siz[e.to];
        if(siz[e.to]>siz[son[t]]||!son[t])
            son[t]=e.to;
    }
}
int xdstot;
int xdsb[maxn];
void dfs2(int u,int topu)
{
    top[u]=topu;
    xdsb[u]=++xdstot;
    if(!son[u])
        return;
    dfs2(son[u],topu);
    for(int i=head[u];i;i=edge[i].next)
    {
        Edge e=edge[i];
        if(e.to==fa[u]||e.to==son[u])
            continue;
        dfs2(e.to,e.to);
    }
}
struct Tree
{
    int l,r;
    int maxx,sum;
}t[maxn*4];
void up(int p)
{
    t[p].maxx=max(t[p<<1].maxx,t[p<<1|1].maxx);
}
void expand(int p,int l,int r)
{
    t[p].l=l;
    t[p].r=r;
    if(l==r)
    {
        t[p].maxx=0;
        return;
    }
    int mid=(l+r)>>1;
    expand(p<<1,l,mid);
    expand(p<<1|1,mid+1,r);
    up(p);
}
void change(int p,int pos,int v)
{
    if(t[p].l==t[p].r)
    {
        t[p].maxx=v;
        return;
    }
    int mid=(t[p].l+t[p].r)>>1; 
    if(pos<=mid)
        change(p<<1,pos,v);
    else
        change(p<<1|1,pos,v);
    up(p);
}
int ask_max(int p,int l,int r)
{
    if(l<=t[p].l&&t[p].r<=r)
    {
        return t[p].maxx;
    }
    int mid=(t[p].l+t[p].r)>>1;
    int ans=0;
    if(l<=mid)
        ans=max(ans,ask_max(p<<1,l,r));
    if(mid+1<=r)
        ans=max(ans,ask_max(p<<1|1,l,r));
    return ans;
}

int find(int x,int y)
{
    int fax=top[x];
    int fay=top[y];
    int ans=0;
    while(fax!=fay)
    {
        if(deep[fax]<deep[fay])
        {
            swap(fax,fay);
            swap(x,y);
        }
        ans=max(ans,ask_max(1,xdsb[fax],xdsb[x]));
        x=fa[fax];
        fax=top[x];
    }
    if(deep[x] > deep[y])
        swap(x,y);
    if(x!=y)
    {
        if(deep[x]>deep[y])
        swap(x,y);
        ans=max(ans,ask_max(1,xdsb[x]+1,xdsb[y]));
    }
    return ans;
}
void clr()
{
    memset(edge,0,sizeof(edge));
    memset(ff,0,sizeof(ff));
    memset(tt,0,sizeof(tt));
    memset(dd,0,sizeof(dd));
    memset(t,0,sizeof(t));
    memset(head,0,sizeof(head));
    memset(son,0,sizeof(son));
    memset(top,0,sizeof(top));
    memset(deep,0,sizeof(deep));
    memset(siz,0,sizeof(siz));
    memset(fa,0,sizeof(fa));
    memset(xdsb,0,sizeof(xdsb));
    tot=0;
    xdstot=0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        clr();
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&ff[i],&tt[i],&dd[i]);
            add(ff[i],tt[i],dd[i]);
            add(tt[i],ff[i],dd[i]);         
        }
        dfs1(20000,1);

        dfs2(1,1);
        expand(1,1,n);
        for(int i=1;i<n;i++)
        {
            if(deep[ff[i]]>deep[tt[i]])
                swap(ff[i],tt[i]);
            change(1,xdsb[tt[i]],dd[i]); 
        }
        char hah[233];
        while(1)
        {
            scanf("%s",hah);
            if(hah[0]=='D')
                break;
            else
            {
                int a,b;
                scanf("%d%d",&a,&b);
                if(hah[0]=='Q')
                    printf("%d\n",find(a,b));
                else
                    change(1,xdsb[tt[a]],b);
            }
        }
    }
    return 0;
}

*/
这里写图片描述
二分(跳石头

#include<cstdio>
using namespace std;
const int maxn=100000+50; 
int num[maxn];
int L,n,m;
int ans;
bool hah(int x)
{
    int cnt=0;
    for(int i=0;i<=n;)
    {
        int t = num[i];
        i ++;
        while((num[i]-t)<x && i <= n) 
        {
            cnt ++;
            i ++;
        }
    }
    if(cnt<=m)//
        return true;
    else
        return false;
}
int ef()
{
    int l=1,r=L;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(hah(mid))
        {
            l=mid + 1;
        }
        else
        {
            r=mid - 1;
        }
    }
    return r;
}
int main()
{
    scanf("%d%d%d",&L,&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    num[++n]=L;
    printf("%d",ef());
    return 0;
}

这里写图片描述
01+完全+多重背包


#include<cstdio>
#include<algorithm> 
using namespace std;
const int maxn=300000+500;
int v[maxn];
int w[maxn];
int m[maxn];
int dp[maxn];
int n,V;
void ly(int vv,int ww)
{
    for(int j=V;j>=vv;j--)
        dp[j]=max(dp[j-vv]+ww,dp[j]);
}
void wq(int vv,int ww)
{
    for(int j=vv;j<=V;j++)
        dp[j]=max(dp[j-vv]+ww,dp[j]);
}
inline void dc(int i)
{
    if(m[i]*v[i]>=V)
    {
        wq(v[i],w[i]);
        return;
    }
    else
    {
        int k=1;
        while(k<m[i])
        {
            ly(k*v[i],k*w[i]);
            m[i]-=k;
            k<<=1;
        }
        ly(m[i]*v[i],m[i]*w[i]);
    }
}
int main()
{
    scanf("%d%d",&n,&V);
    for(int i=1;i<=n;i++)
        scanf("%d%d%d",&v[i],&w[i],&m[i]);
    for(int i=1;i<=n;i++)
    {
        if(m[i]==1)
            ly(v[i],w[i]);
        else if(m[i]==-1)
            wq(v[i],w[i]);
        else
            dc(i);
    }
    int ans=0;
    for(int i=0;i<=V;i++)
    ans=max(ans,dp[i]);
    printf("%d",ans);
    return 0;
}

这里写图片描述
模大质数情况下的逆元

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int ha=10000007; 
int ksm(int x,int y)
{
    if(y==1) return x%ha;
    if(y==0) return 1;
    int hah=ksm(x,y/2)%ha;
    if(y%2==0)
    return ((hah%ha)*(hah%ha))%ha;
    else
    return (((hah%ha)*(hah%ha)%ha)*(x%ha))%ha;

}
int inverse(int b)
{
    return ksm(b,ha-2)%ha;
} 
int main()
{
    int n,m;
    cin>>n>>m;
    int ans=inverse(m)%ha;
    cout<<n/m<<endl;
    cout<<n*ans;
    return 0;
}

这里写图片描述
对拍器

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tot=1;
    while(1)
    {
        printf("Case:%d",tot++);
        system("data.exe");
        system("std.exe");
        system("baoli.exe");
        if(!system("fc std.txt baoli.txt"))
            puts("AC");
        else
            system("pause");
    }
    return 0;
}

这里写图片描述
map(dcOJ expedition

#include<cstdio>
#include<map>
using namespace std;
typedef unsigned long long ll;
ll n;
map<ll,ll>mm;
map<ll,ll>::iterator it;
void read(ll &a)
{
    char c=getchar();
    ll flag=1;
    while(c<'0'||c>'9')
    {
        if(c=='-')
            flag=-1;
        c=getchar();
    }
    ll ans=0;
    while(c<='9'&&c>='0')
        ans=ans*10+c-'0',c=getchar();
    a=ans*flag;
    return;
}
int main() 
{
    freopen("expedition.in","r",stdin);
    freopen("expedition.out","w",stdout);
    read(n);
    for(ll i=1;i<=n;i++)
    {
        ll a,b;
        read(a),read(b);
        mm[b]+=a;
    }
    it=mm.begin();
    ll ans=0;
    for(;it!=mm.end();it++)
    {
        ll hah=it->second; 
        ans+=hah*hah;
    }
    printf("%llu",ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

exgcd(CODEVS 解的个数

#include<cstdio>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    ll r=exgcd(b,a%b,x,y);
    ll t=x;
    x=y;
    y=t-(a/b)*y;
    return r;
}
int main()
{
    int tott=0;
    int n;
    scanf("%d",&n);
    while(n--)
    {
        ll a,b,c,p,q,r,s;
        ll x,y;
        scanf("%lld%lld%lld%lld%lld%lld%lld",&a,&b,&c,&p,&q,&r,&s);

        if(q<p||s<r)
        {
            puts("0");
            continue;
        }
        if(a==b&&a==0)
        {
            if(c==0)
                printf("%lld\n",(q-p+1)*(s-r+1));
            else
                puts("0");
            continue;
        }
        c*=-1;
        ll hah=exgcd(a,b,x,y);
        if(c%hah)
        {
            puts("0");
            continue; 
        } 
        int tot=0;
        if(b==0)
        {
            while(y>=r)
                y-=a;
            while(y<=s)
            {
                y+=a;
                if((y*b==c)&&(r<=y&&y<=s))
                    tot++;
            }
            printf("%d\n",tot);
        }
        else
        {
            a/=hah,b/=hah,c/=hah;
            x*=c,y*=c;
            while(x>=p)
                x-=b,y+=a;
            while(x<=q)
            {
                x+=b,y-=a;
                if((p<=x&&x<=q)&&(r<=y&&y<=s))
                    tot++;

            }
            printf("%d\n",tot);
        }

    }
    return 0;
}

埃氏筛法

#include<cstdio>
using namespace std;
const int maxn=1000000;
bool sus[maxn];
int ss[maxn];
int cnt=0;
int n;
int main()
{
    scanf("%d",&n);
    for(int i=2;i<=n;i++)
    {
        if(!sus[i])
        {
            ss[++cnt]=i;
            int x=i;
            int t=2;
            while(x*t<=maxn)
            {
                sus[x*t]=1;
                t++;
            }
        }
        else
            continue;
    }
    for(int i=1;i<=cnt;i++)
        printf("%d\n",ss[i]);
}

快速幂(递归

#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll; 
const ll ha=1e9+7;
ll ksm(ll x,ll y)
{
    if(y==0)    return 1;
    if(y==1)    return x%ha;
    ll hah=ksm(x,y>>1);
    if(y&1)     return (((hah%ha)*(hah%ha))%ha*(x%ha));
    return ((hah%ha)*(hah%ha))%ha;
}
ll n,m;
int main()
{
    scanf("%lld%lld",&n,&m);
    printf("%lld",ksm(n,m));        
}

子集枚举

#include<bits/stdc++.h>
using namespace std;
const int maxn=50; 
bool vis[maxn];
int n;
void dfs(int tmp,int cnt)
{
    if(cnt>n)
    {
        for(int i=1;i<=n;i++)
        {
            if(!i[vis])
            printf("%d ",i);
        }
        puts("");
        return;
    }
    for(int i=tmp;i<=n;i++)
    {
        if(!i[vis])
        {
            i[vis]=1;
            dfs(i+1,cnt+1);
            i[vis]=0;
            dfs(i+1,cnt+1);
        }
    }
}
int main()
{
    scanf("%d",&n);
    dfs(1,1);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值