Codeforces Round #209 (Div. 2)
来源:互联网 发布:网页游戏劫持数据 编辑:程序博客网 时间:2024/05/17 18:47
题目链接
A. Table
题意:给n*m的网格,问最少需要涂几次能把网格全涂满,每次涂的范围是一个corner和一个为1的格子所组成的矩形.
思路:可以分析到若有1的格子在边上,那么最多只需要2次,若没有1的格子在边上,那么题目说一定有解,那么答案就是4次
#include<bits/stdc++.h>using namespace std;int a[55][55];int main(){ int n,m,flag=0; scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ scanf("%d",&a[i][j]); if(a[i][j]==1&&(i==0||i==n-1||j==0||j==m-1)){ printf("2\n"); return 0; } } } printf("4\n");}
B. Permutation
题意:输入n,k.求满足该等式.的数列a.
思路:|1-2|+|3-4|-|1-2+3-4|=|2-1|+|4-3|-|2-1+4-3|=0, 观察到若是这样|2-1|+|3-4|-|2-1+3-4|=2=2*1,
所以只需要将正常排序的1-n的数列,将前k组a(2*i-1)与a(2*i)交换即可.
#include<bits/stdc++.h>using namespace std;int a[100005];int main(){ int n,k; scanf("%d%d",&n,&k); for(int i=1;i<=2*n;i++){ a[i]=i; } for(int i=1;i<=k;i++){ swap(a[2*i-1],a[2*i]); } for(int i=1;i<=2*n;i++) printf("%d ",a[i]); printf("\n");}
C. Prime Number
题意:输入n,x,长度为n的数组a.=,求gcd(s,t)%1e9+7.
思路:可以分析到gcd(s,t)一定是x^m这样的形式.分母t=x^(a1+a2+a3+...+an);s=x^(a2+a3+...+an)+x^(a1+a3+...+an)+...+x^(a1+a2+a3+...+an-1);m=min((a2+a3+...+an),(a1+a3+...+an),...,(a1+a2+...+an)).但是若有重复的指数则要叠加.(这里要注意不能直接将重复指数的个数/x加上去.因为一旦出现重复指数的个数为奇数时必定会落单一个该指数.)
#include<bits/stdc++.h>using namespace std;typedef long long LL;priority_queue<LL,vector<LL>,greater<LL> >Q;const LL MOD=1e9+7;LL a[100005],b[100005];bool cmp(LL a,LL b){ return a>=b;}LL multi_mod(LL a,LL b){ LL ans=0; while(b){ if(b&1){ ans=(ans+a)%MOD; } a<<=1; if(a>=MOD) a%=MOD; b>>=1; } return ans;}LL pow_mod(LL a,LL b){ LL ans=1; while(b){ if(b&1){ ans=multi_mod(ans,a)%MOD; } a=multi_mod(a,a)%MOD; b>>=1; } return ans%MOD;}int main(){ LL n,x,sum=0,m; scanf("%lld%lld",&n,&x); for(int i=0;i<n;i++){ scanf("%lld",a+i); sum+=a[i]; } m=sum; for(int i=0;i<n;i++){ b[i]=sum-a[i]; Q.push(b[i]); } LL f,num,l; while(1){ num=0; f=Q.top(); while(Q.top()==f&&!Q.empty()){ num++; Q.pop(); } if(num%x){ printf("%lld\n",pow_mod(x,min(sum,f))); return 0; } else{ for(int i=0;i<num/x;i++)Q.push(f+1); } }}
D. Pair of Numbers
题意:给出长度为n的数组a,寻找l,r满足(r-l)最大且有l<=j<=r. l,r范围内的各个ai使得ai%aj==0,输出满足这两个条件的(l,r)的个数,以及r-l最大值,还要输出各个l.
思路:枚举每个ai,向左向右寻找满足这两个条件的l,r不断更新.
#include<bits/stdc++.h>using namespace std;int a[300005],ans[300005];int main(){ int n,l=0,r=0,m,MAX=0,k=0,cnt=0; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",a+i); } while(k<n){ l=k,r=k; while(l>=0&&!(a[l]%a[k]))l--; while(r<=n-1&&!(a[r]%a[k]))r++; k=r; int len=r-l-2; if(len>MAX)MAX=len,cnt=0; if(len==MAX)ans[cnt++]=l+2; } printf("%d %d\n",cnt,MAX); for(int i=0;i<cnt-1;i++){ printf("%d ",ans[i]); } printf("%d\n",ans[cnt-1]); }
- Codeforces Round #209 (Div. 2)
- Codeforces Round #209 (Div. 2)
- Codeforces Round #209 (Div. 2)
- Codeforces Round #209 (Div. 2) A. Table
- Codeforces Round #209 (Div. 2) B. Permutation
- Codeforces Round #209 (Div. 2) <A-D>
- Codeforces Round #209 (Div. 2)思路
- Codeforces Round #209 (Div. 2) A.Table
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #107 (Div. 2)
- Codeforces Round #108 (Div. 2)
- Codeforces Round #110 (Div. 2)
- Codeforces Round #122 (Div. 2)
- Codeforces Round #202 (Div. 2) B. Color the Fence(贪心)
- Linux的五种I/O模型
- Validate Binary Search Tree
- 聪哥的壁纸
- ossec主要功能介绍
- Codeforces Round #209 (Div. 2)
- 字符指针的初始化
- 背包相关问题
- JAVA项目一:图形化界面计算器
- 机器学习笔记:Fisher Vector基本原理与用法
- pthread第一次使用
- 用c++实现红黑树的判断、插入、遍历操作
- 基于Heros的过程间数据流分析的相关API
- select函数详解及其应用