七月训练汇总-模板

        快速幂

#include<bits/stdc++.h>

typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;
const ll MOD = 987654321;

ll BP(ll a,ll b)
{
    a %= MOD;
    ll ans = 1;
    while(b>0)
    {
        if(b&1)
            ans = ans * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    ll a,b;
    cin>>a>>b;
    ll ans = BP(a,b);
    cout<<ans<<'\n';
    return 0;
}

        GCD和LCM

#include<bits/stdc++.h>

typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

//对于两个数,他们的LCM等于两数乘积除以GCD

//LCM(a,b) = a * b / GCD(a,b)

ll GCD(int a,int b)
{
    if(a%b == 0)    
        return b;
    else    
        return GCD(b,a % b);
}

ll LCM(ll a,ll b)
{
    ll temp = a*b;
    temp /= GCD(a,b);
    return temp;
}

int main()
{
    ll a,b;
    cin>>a>>b;
    ll ans = LCM(a,b);
    cout<<ans<<'\n';
}

        欧拉筛法

#include<bits/stdc++.h>
typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

vector<int>pri;
bool not_prime[M];

void pre(int n)
{
    for(int i = 2;i<=n;++i)
    {
        if(!not_prime[i])
            pri.push_back(i);
        for(int pri_j : pri)
        {
            if(i * pri_j > n)   break;
            not_prime[i * pri_j] = true;
            if(i % pri_j == 0)  break; 
        }
    }
}

int main()
{
    int n;
    cin>>n;
    pre(n);
    sort(pri.begin(),pri.end(),greater<int>());
    while(!pri.empty())
    {
        cout<<pri.back()<<' ';
        pri.pop_back();
    }
}

        埃式筛法 

#include<bits/stdc++.h>
typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

vector<ll>prime;
bool is_prime[M];

void Eratos(ll n)
{
    is_prime[0] = is_prime[1] = false;
    for(ll i = 2;i<=n;++i)
        is_prime[i] = true;
    for(ll i = 2;i * i<=n;++i)
        if(is_prime[i])
            for(ll j = i*i;j<=n;j+=i)
                is_prime[j] = false;
    for(int i = 2;i<=n;++i)
        if(is_prime[i])
            prime.push_back(i);
}

int main()
{
    Eratos(505);
    for(int i = 0;i<prime.size();i++)
        cout<<prime[i]<<' ';
}

        单个数的欧拉函数值

#include<bits/stdc++.h>
typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

/*
    欧拉函数    phi(x)
    表示的是小于等于n并且与n互质的数的个数。
    当x为质数的时候,phi(x) = x-1
*/

//求一个数的欧拉函数值
ll phi(ll n)
{
    ll ans = n;
    for(ll i = 2;i*i<=n;++i)
        if(n%i == 0)
        {
            ans = ans / i * (i-1);
            while(n % i == 0)
                n/=i;
         
    if(n>1)
        ans = ans / n * (n-1);
    return ans;
}

int main()
{
    ll n;
    cin>>n;
    n的欧拉函数值
    cout<<phi(n)<<'\n';
}  

        从1到n所有数字的欧拉函数值

#include<bits/stdc++.h>
typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

vector<ll>pri;
bool not_prime[M];
int phi[M];

void pre(ll n)
{
    phi[1] = 1;
    for(int i = 2;i<=n;++i)
    {
        if(!not_prime[i])
        {
            pri.push_back(i);
            phi[i] = i-1;
        }
        for(int pri_j : pri)
        {
            if(i*pri_j>n)   
                break;
            not_prime[i*pri_j] = true;
            if(i%pri_j == 0)
            {
                phi[i*pri_j] = phi[i]*pri_j;
                break;
            }
            phi[i*pri_j] = phi[i]*phi[pri_j];
        }
    }
}
int main()
{
    ll n;
    cin>>n;
    // 1到n的所有数的欧拉函数值
    pre(n);
    for(int i = 1;i<=n;++i)
        cout<<phi[i]<<' ';
}   

        十进制转二进制

#include<bits/stdc++.h>

typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

stack<ll>st;

void DtoB(ll n)
{
    while(n)
    {
        st.push(n % 2);
        n>>=1;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    ll n;
    cin>>n;
    DtoB(n);
    while(st.size())
    {
        cout<<st.top();
        st.pop();
    }
}

        二进制转十进制

#include<bits/stdc++.h>

typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

ll BP(ll a,ll b)
{
    ll ans = 1;
    while(b>0)
    {
        if(b&1)
            ans = ans * a;
        a = a * a;
        b >>= 1;
    }
    return ans;
}

ll ans = 0;

void BtoD(string s)
{
    for(int i = s.size()-1;i>=0;i--)
        ans+=(s[i]-'0')*BP(2,s.size()-1-i);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    string s;
    cin>>s;
    BtoD(s);
    cout<<ans<<'\n';
}

         十进制转十六进制

#include<bits/stdc++.h>

typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

string hx = "0123456789ABCDEF";
string s = "";

void DtoH(ll n)
{
    ll p;
    while(n)
    {
        p = n % 16;
        s = hx[p] + s;
        n /= 16;
    }
}

int main()
{
    ll n,p;
    cin>>n;
    DtoH(n);
    if(s == "")
        cout<<0<<'\n';
    else
        cout<<s<<'\n';
}

        十六进制转十进制

#include<bits/stdc++.h>

typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

ll d;

void HtoD(string s)
{
    ll d = stoi(s,0,16);
}

int main()
{
    string s;
    cin>>s;
    HtoD(s);
    cout<<d<<'\n';
}

        八皇后问题

#include<bits/stdc++.h>
typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;
const int MOD = 1000000007;

//八皇后问题,输出可行解的个数

int n;
int tot = 0;
int vis[3][20];

void dfs(int pos)
{
    if(pos == n)
        tot++;
    else
        for(int i = 0;i<n;i++)
            if(!vis[0][i] && !vis[1][pos+i] && !vis[2][pos-i+n])
            {
                vis[0][i] = vis[1][pos+i] = vis[2][pos-i+n] = 1;
                dfs(pos+1);
                vis[0][i] = vis[1][pos+i] = vis[2][pos-i+n] = 0;
            }
}

int main()
{
    cin>>n;
    memset(vis,0,sizeof(vis));
    /*n为棋盘大小*/
    dfs(0);
    cout<<tot<<'\n';
}

        威佐夫博弈

#include<bits/stdc++.h>

typedef __int64 ll;

using namespace std;

const int M = 200005;
const int INF = 0x7f7f7f7f;

//Question discription

/*
    有两堆物品,两个人轮流从任意一堆中取出至少一个或者从两堆中取出同样多的物品
    每次至少取一个,至多不限。
    最后取完物品的人胜利
*/

//The low of the gambling

/*
    先手必输的规律:
    两堆物品数:
    0 0
    1 2
    3 5
    4 7
    6 10
    8 13
    9 15
    11 18
    小堆的数量为两堆差值*1.618
*/

int main()
{
    //n,m为两堆物品数量
    int n,m;
    cin>>n>>m;
    int p = min(n,m);
    int q = max(n,m);
    double r = double(q-p);
    double w = (sqrt(5.0)+1)/2;
    int temp = int(r*w);
    if(temp == p)
        cout<<"后手胜"<<'\n';
    else
        cout<<"先手胜"<<'\n';
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值