码蹄集24周周赛(史莱姆融合,求逆矩阵,矩阵游戏,矩阵计数,比较器)

史莱姆融合

思路:双向并查集,每次找出来正向和反向的老大,然后两个连通块连接起来,跑并查集要路径压缩,不然回T。然后DFS遍历

/*
 * @Author: 晚乔最美 
 * @Date: 2022-11-09 19:32:46 
 * @Last Modified by:   晚乔最美 
 * @Last Modified time: 2022-11-10 15:25:17 
 */
#include<bits/stdc++.h>
#include<bitset>
#include<unordered_map>
#define pb push_back
#define bp __builtin_popcount
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#define ls x<<1
#define rs x<<1|1
using namespace std;
typedef  long long ll;
const int inf=0x3f3f3f3f;
const int maxn=2e6+10;
const int MOD=1e9+7;
const int mod =1e9+7;
const double PI=3.14;
int lowbit(int x){return x&-x;}
ll gcd(ll x, ll y){ return y == 0 ? x : gcd(y, x%y); }
ll lcm(ll x, ll y){ return x / gcd(x, y)*y; }
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }
int fa[maxn];
int rfa[maxn];
vector<int> G[maxn];

inline 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 << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
int find(int root)//向上找
{
    int son,temp;
    son=root;
    while(root!=fa[root])
    {
        root=fa[root];
    }

    while(son!=root)//路径压缩
    {
        temp=fa[son];
        fa[son]=root;
        son=temp;
    }
    return root;
}
int refa(int root)//向下找
{
    int son,temp;
    son=root;
    while(root!=rfa[root])
    {
        root=rfa[root];
    }

    while(son!=root)
    {
        temp=rfa[son];
        rfa[son]=root;
        son=temp;
    }
    return root;
}
void dfs(int x)
{
    cout << x << " ";
    for (int i = 0; i < G[x].size();i++)
    {
        dfs(G[x][i]);
    }
}
int main()
{
    int n;
    n=read();
    for (int i = 1; i <= n;i++)
    {
        fa[i] = i;
          rfa[i] = i;
    }
    int x, y;
    for (int i = 1; i < n;i++)
    {

        x=read();
        y=read();

        int f = find(y);
        int rf=refa(x);
        fa[f] = rf;

        rfa[rf] = f;

        G[rf].push_back(f);
    }
    int pos = 0;
    for ( int i = 1; i <= n;i++)
    {
        if(fa[i]==i)
        {
            pos = i;
            break;
        }
    }

    dfs(pos);
     //system("pause");
    return 0;
}
/*

5
1 4
2 5
3 1
4 5

3 1 4 2 5
*/

求逆矩阵

暂时没写出来

矩阵游戏

思路:不难看出当一行的开头固定的时候,这一行就是确定的了,所有就是第一列所有组合的方案数,每个格子两种求法,不难想到快速幂求2^n,但是根据题意发现n的取值是1e1000000,不难想到欧拉降幂
降幂公式为,在这里插入图片描述
,欧拉函数为在这里插入图片描述
其中pi为n的所有质因子,可以用唯一分解定理求得

AC代码为

/*
 * @Author: 晚乔最美 
 * @Date: 2022-11-09 18:30:26 
 * @Last Modified by:   晚乔最美 
 * @Last Modified time: 2022-11-09 19:17:18 
 */
#include<bits/stdc++.h>
#include<bitset>
#include<unordered_map>
#define pb push_back
#define bp __builtin_popcount
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#define ls x<<1
#define rs x<<1|1
using namespace std;
typedef  long long ll;
const int inf=0x3f3f3f3f;
const int maxn=3e4+10;
const int MOD=1e9+7;
const int mod = 1e9+7;
const double PI=3.14;
int lowbit(int x){return x&-x;}
ll gcd(ll x, ll y){ return y == 0 ? x : gcd(y, x%y); }
ll lcm(ll x, ll y){ return x / gcd(x, y)*y; }
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

ll euler(ll n)
{
    ll ans = n;
    for (int i = 2; i <= sqrt(n + 0.5);i++)
    {
           if(n%i==0)
           {
               ans = ans / i * (i - 1);
               while(n%i==0)
               {
                   n /= i;
               }
           }
    }

    if(n>=2)
        ans = ans / n * (n - 1);

    return ans;
}
//2^n%mod=2^(n%euler(n)+erler(n))

ll euler_drop_pow(string st)
{
    ll ans = 0;

    ll eulernum = euler(mod);

    for (int i = 0; i < st.length();i++)
    {
        ans = ans * 10 + st[i] - '0';

        ans = ans % eulernum;

    }

    ans = ans + eulernum;

    return dpow(2, ans);
}

int main()
{
    string n, m;
    cin >> n >> m;
    ll ans = euler_drop_pow(n);

    cout << ans << endl;

    //system("pause");

    return 0;


}

/*
3 5

8


*/

矩阵计数

我写的一直T,用的范老师的代码

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    bool boxes[302][302];
    int n;
    cin>>n;
    string temp;
    for(int i=0;i<n;i++){
        cin>>temp;
        for(int j=0;j<temp.size();j++)
            boxes[i][j]=bool(temp[j]-'0');
        
    }
    int sum=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(boxes[i][j]){
                for(int x=i+1;x<n;x++){
                    for(int y=j+1;y<n;y++){
                        if(boxes[i][j]&&boxes[i][y]&&boxes[x][j]&&boxes[x][y])
                            sum++;
                    }
                }
            }
        }
    }
    cout<<sum;
    return 0;
}

比较器

/*
 * @Author: 晚乔最美 
 * @Date: 2022-11-10 15:30:26 
 * @Last Modified by:   晚乔最美 
 * @Last Modified time: 2022-11-10 19:30:26 
 */
#include<bits/stdc++.h>
#include<bitset>
#include<unordered_map>
#define pb push_back
#define bp __builtin_popcount
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#define ls x<<1
#define rs x<<1|1
using namespace std;
typedef  long long ll;
const int inf=0x3f3f3f3f;
const int maxn=310;
const int MOD=1e9+7;
const int mod =1e9+7;
const double PI=3.14;
int lowbit(int x){return x&-x;}
ll gcd(ll x, ll y){ return y == 0 ? x : gcd(y, x%y); }
ll lcm(ll x, ll y){ return x / gcd(x, y)*y; }
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }
int f[maxn][maxn];
char a[maxn][maxn];
vector<int> G[maxn];

inline 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 << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
int main()
{
    int x,y;
    cin >> x >> y;
    if(x*log10(y)-y*log10(x)>0)
        cout << -1 << endl;
    else if(x*log10(y)-y*log10(x)<0)
        cout << 1 << endl;
    else
        cout << 0 << endl;

    //system("pause");
    return 0;
}
/*
xlny-ylnx
*/
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值