【C++】信息竞赛自用模板

发布于 2023-01-23  368 次阅读


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){ if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){ x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
long long f(int x,int y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x;
        x=x*x;
        y>>=1;
    }
    return ans;
}
int exgcd(int a,int b)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int r=exgcd(b,a%b);
    k=x;
    x=y;
    y=k-a/b*y;
    return r;
}
int main(int argc, char** argv) {
    memset(isprime,1,sizeof(isprime));
    for(i=2;i<=n;++i)
    {
        if(isprime[i])
            for(j=2*i;j<=n;j=j+i)
                isprime[j]=0;
    }
    for(i=2;i<=n;++i)
        if(isprime[i])
            cout<<i<<" ";
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
const int N=2005;
int c[N][N];
void inverse(int n,int p)
{
    int i;
    inv[1]=1;
    for(i=2;i<=n;++i)
        inv[i]=(p-p/i)*inv[p%i]%p;
}
int main(int argc, char** argv) {
    int i,j,n;
    //
    c[0][0]=1;
    for(i=1;i<N;++i)
        c[i][0]=c[i][i]=1;
    for(i=1;i<N;++i)
        for(j=1;j<i;++j)
            c[i][j]=c[i-1][j]+c[i-1][j-1];
    cout<<c[4][1]<<" "<<c[4][2];
    //  
    //
    cin>>n;
    c[0]=1;
    inverse(N,p);
    for(i=1;i<=n;++i)
        //c[i]=c[i-1]*(n-i+1)/i;
        c[i]=((c[i-1]*(n-i+1)%p)*inv[i])%p;
    cout<<c[1]<<" "<<c[2];
    //
    return 0;
}
int getl(int a,int b)
{
    int len=0,cur=0;
    if(l[a]<l[b]) swap(a,b);
    for(int i=0;i<l[a];++i)
    {
        for(int j=0;j<l[b];j++)
        {
            int k=i,ll=j;
            for(;k<l[a] && ll<l[b];++k,++ll)
            {
                if(s[a][k]==s[b][ll]) ++cur;
                else break;
            }
            len=max(cur,len);
            cur=0;
        }
    }
    return len;
}
#include<stdio.h>
#include<string.h>
char a[30],b[30];
int lena,lenb;
int LCS(int,int);  ///两个参数分别表示数组a的下标和数组b的下标

int main()
{
    strcpy(a,"ABCBDAB");
    strcpy(b,"BDCABA");
    lena=strlen(a);
    lenb=strlen(b);
    printf("%d\n",LCS(0,0));
    return 0;
}

int LCS(int i,int j)
{
    if(i>=lena || j>=lenb)
        return 0;
    if(a[i]==b[j])
        return 1+LCS(i+1,j+1);
    else
        return LCS(i+1,j)>LCS(i,j+1)? LCS(i+1,j):LCS(i,j+1);
}
届ける言葉を今は育ててる
最后更新于 2023-01-23