前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforce #566 A B C(模拟) E(矩阵快速幂+欧拉降幂)

Codeforce #566 A B C(模拟) E(矩阵快速幂+欧拉降幂)

作者头像
用户2965768
发布2019-07-04 10:44:35
5580
发布2019-07-04 10:44:35
举报
文章被收录于专栏:wymwym

A.显然

代码语言:javascript
复制
#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int maxn = 200005;
int main()
{
	int n;
	cin>>n;
	if(n%2==0){
		n/=2;
		cout<<(1<<n)<<endl;
	}else {
		cout<<0<<endl;
	}
	return 0;
}

B.易得

代码语言:javascript
复制
//B
#include <bits/stdc++.h>
#define ll long long 
using namespace std;
char s[505][505];
int main()
{
	int n,m,dian = 0;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>s[i];
		for(int j=0;j<m;j++){
			if(s[i][j]=='*')dian++;
		}
	}	
	int cent = 0,ni,nj,flag;
	for(int i=0;i<n;i++){
		
		for(int j=0;j<m;j++){
			flag = 1;
			if(s[i][j]=='*'); else flag = 0;
			if(i-1>=0&&s[i-1][j]=='*'); else flag = 0;
			if(j-1>=0&&s[i][j-1]=='*');	else flag = 0;
			if(j+1<m&&s[i][j+1]=='*'); else flag = 0;
			if(i+1<n&&s[i+1][j]=='*'); else flag = 0;
			if(flag){
				cent++;
				ni = i;
				nj = j;
			}
		}
		
	}
	if(cent!=1){
		printf("NO\n");
	}else {
		int tpi = ni,tpj = nj;
		while(tpi>=0&&s[tpi][tpj]=='*'){
			dian--;		tpi--;	
		}
		tpi = ni+1; tpj = nj;
		while(tpi<n&&s[tpi][tpj]=='*'){
			dian--;		tpi++;
		}
		tpi = ni;	tpj = nj - 1;
		while(tpj>=0&&s[tpi][tpj]=='*'){
			dian--;		tpj--;
		}
		tpi = ni;	tpj = nj + 1;
		while(tpj<m&&s[tpi][tpj]=='*'){
			dian--;		tpj++; 
		}
		if(dian==0){
			printf("YES\n");
		}else printf("NO\n");
	}
	return 0;
}

C.将数量相同但不是同一个结尾的放一个vector里,数量相同结尾相同放另一个vector里。

代码语言:javascript
复制
//C
#include <bits/stdc++.h>
#define ll long long 
using namespace std;
int n;
struct Node{
	string s;
	int num;
	char last;
}node[100005];
int cmp(Node a,Node b){
	if(a.num==b.num){
		return a.last<b.last;
	}else {
		return a.num<b.num;
	}
}
vector<pair<int,int> > pa1,pa2;
int main()
{
	string s;
	int num,size;
	char last;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s; num = 0; size = s.size();
		for(int i=0;i<size;i++){
				if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'){
					num++;	last = s[i];
				}
		} 
		node[i].num = num,node[i].last = last,node[i].s = s;
	}
	sort(node,node+n,cmp);
	int ls=-1;
	int cnt = 1,pos = 0;
	while(pos<n){
		if(node[pos].num==node[pos+1].num&&node[pos].last==node[pos+1].last){
			pa1.push_back(make_pair(pos,pos+1));
			pos+=2;
		}else if(cnt!=node[pos].num){
			ls = pos;
			cnt = node[pos].num;
			pos++;
		}else if(ls==-1){
			ls = pos;
			cnt = node[pos].num;
			pos++;
		}else {
			pa2.push_back(make_pair(pos,ls));
			ls = -1;
			pos++;	
		}
	}	
	int ans,s1=pa1.size(),s2=pa2.size();	
	ans = min(s1,s2) + max(0,(s1-s2)/2);
	cout<<ans<<endl;
	for(int i=0;i<min(s1,s2);i++){
		cout<<node[pa2[i].first].s<<" "<<node[pa1[i].first].s<<endl;
		cout<<node[pa2[i].second].s<<" "<<node[pa1[i].second].s<<endl;
	}
	if(s1>s2){
			for(int i=min(s1,s2);i+1<s1;i+=2){
			cout<<node[pa1[i+1].first].s<<" "<<node[pa1[i].first].s<<endl;
			cout<<node[pa1[i+1].second].s<<" "<<node[pa1[i].second].s<<endl;
		}
	}
	return 0;
}

E. 矩阵快速幂+欧拉降幂

第n项中f1的指数是fn-1+fn-2+fn-3之和,由此构造 。f2,f3同理

第n项中c的指数是 fn-1+fn-2+fn-3+2*i-6 ,构造矩阵

代码语言:javascript
复制
//E
#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long 
using namespace std;
struct Mat{
	ll m[101][101];
};
const ll mod = 1000000007;
int n;
Mat a;
Mat g,cx; 
Mat mul(Mat x,Mat y,ll mo,int bound){
	Mat cc;
	for(int i=1;i<=bound;i++){
		for(int j=1;j<=bound;j++){
			cc.m[i][j] = 0;	
			for(int k=1;k<=bound;k++){
				cc.m[i][j] = (cc.m[i][j]+ x.m[i][k]*y.m[k][j]%mo)%mo;
			}
		}
	}
	return cc;
}
Mat pow(Mat x,ll y,ll mo,int bound){
	Mat ans;
	for(int i=1;i<=bound;i++)
		for(int j=1;j<=bound;j++)
		if(i==j)ans.m[i][i] = 1;
		else ans.m[i][j] = 0;
	while(y){
		if(y&1)ans = mul(ans,x,mo,bound);
		x = mul(x,x,mo,bound);
		y>>=1;
	}
	return ans;
}
ll quick(ll x,ll y){
	ll re = 1;
	while(y){
		if(y&1)re = re*x%mod;
		x=x*x%mod;
		y>>=1; 
	}
	return re;
}
int main(){	
	ll n,f1,f2,f3,c;
	cin>>n>>f1>>f2>>f3>>c;
	g.m[1][1] = 1;	g.m[1][2] = 1;	g.m[1][3] = 1;
	g.m[2][1] = 1;	g.m[2][2] = 0;	g.m[2][3] = 0;
	g.m[3][1] = 0;	g.m[3][2] = 1;	g.m[3][3] = 0;
	cx.m[1][1] = 1;	cx.m[1][2] = 1;	cx.m[1][3] = 1;	cx.m[1][4] = 1;	cx.m[1][5] = 0;
	cx.m[2][1] = 1;	cx.m[2][2] = 0;	cx.m[2][3] = 0;	cx.m[2][4] = 0;	cx.m[2][5] = 0;
	cx.m[3][1] = 0;	cx.m[3][2] = 1;	cx.m[3][3] = 0;	cx.m[3][4] = 0;	cx.m[3][5] = 0;
	cx.m[4][1] = 0;	cx.m[4][2] = 0;	cx.m[4][3] = 0;	cx.m[4][4] = 1;	cx.m[4][5] = 2;
	cx.m[5][1] = 0;	cx.m[5][2] = 0;	cx.m[5][3] = 0;	cx.m[5][4] = 0;	cx.m[5][5] = 1;
	ll	exp_f1,exp_f2,exp_f3,num_c;

	g =  pow(g,n-3,1e9+6,3);

	exp_f1 = g.m[1][3];
	exp_f2 = g.m[1][2];
	exp_f3 = g.m[1][1];

	
	cx = pow(cx,n-3,1e9+6,5);
	num_c = (cx.m[1][4]*2 + cx.m[1][5])%(mod-1);

	ll ans=0;
	ans=quick(f1,exp_f1)%mod;
	ans=ans*(quick(f2,exp_f2)%mod)%mod;
	ans=ans*(quick(f3,exp_f3)%mod)%mod;
	ans=ans*(quick(c,num_c)%mod)%mod;
	cout<<ans<<endl;
	return 0;
}
/*
5 1 2 5 3

*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年07月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档