http://blog.csdn.net/rowanhaoa/article/details/38116713

A:Game With Sticks

水题。。

每次操作,都会拿走一个横行,一个竖行。

所以一共会操作min(横行,竖行)次。

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<map>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define mem(a,b) (memset(a),b,sizeof(a))
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now int l,int r,int rt
#define INF 99999999
#define LL __int64
#define mod 1000000009
#define eps 1e-6
#define zero(x) (fabs(x)<eps?0:x)
#define maxn 330000
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int y=min(n,m);
if(y%2)cout<<"Akshat"<<endl;
else cout<<"Malvika"<<endl;
}
return 0;
}

B:Sort the Array

给数组中的每个数字标号,标记他应该出如今哪一个位置。

然后从头往后找,假设当前位置的数字不是应该在这个位置的数字。

那么就找到应该在这个位置的数字的位置,然后翻转。

假设还不是按顺序排的话,就输出no。否则输出yes。

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<map>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define mem(a,b) (memset(a),b,sizeof(a))
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now int l,int r,int rt
#define INF 99999999
#define LL __int64
#define mod 1000000009
#define eps 1e-6
#define zero(x) (fabs(x)<eps?0:x)
#define maxn 110000
struct list
{
int x;
int id;
int y;
}node[maxn];
int cmp1(list a,list b)
{
return a.x<b.x;
}
int cmp2(list a,list b)
{
return a.id<b.id;
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&node[i].x);
node[i].id=i;
}
sort(node+1,node+n+1,cmp1);
for(int i=1;i<=n;i++)node[i].y=i;
sort(node+1,node+n+1,cmp2);
int st,ed,i;
st=ed=-1;
for(i=1;i<=n;i++)
{
if(node[i].y!=i)
{
if(st==-1)
{
int j,k;
for(j=i+1;j<=n;j++)
{
if(node[j].y==i)break;
}
for(k=j;k>=i;k--)
{
if(node[k].y!=i+(j-k))break;
}
if(k>=i)break;
st=i;
ed=j;
i=j+1;
}
else break;
}
}
if(i<=n)cout<<"no"<<endl;
else
{
cout<<"yes"<<endl;
if(st!=-1)cout<<st<<" "<<ed<<endl;
else cout<<"1 1"<<endl;
}
}
return 0;
}

C:Predict Outcome of the Game

枚举两个差值的正负号。

对于每一种情况:

能够算出A,B,C最少赢几个球。

然后看看当前的赢球数是不是符合K。

然后看一下差值能否够用(n-k)消除掉。

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<map>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define mem(a,b) (memset(a),b,sizeof(a))
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now int l,int r,int rt
#define INF 99999999
#define LL __int64
#define mod 1000000009
#define eps 1e-6
#define zero(x) (fabs(x)<eps?0:x)
#define maxn 110000
struct list
{
int x;
int id;
int y;
}node[maxn];
int cmp1(list a,list b)
{
return a.x<b.x;
}
int cmp2(list a,list b)
{
return a.id<b.id;
}
int pan(LL n,LL k,LL d1,LL d2)
{
LL a,b,c;
a=0;
b=a+d1;
c=b+d2;
LL d;
d=min(a,min(b,c));
if(d<0)
{
d=-d;
a+=d;
b+=d;
c+=d;
}
d=max(a,max(b,c));
LL cha=0;
cha=a+b+c;
cha=k-cha;
if(cha<0)return 0;
if(cha%3)return 0;
cha=0;
cha+=d-a;
cha+=d-b;
cha+=d-c;
LL cun=n-k;
cun=cun-cha;
if(cun<0)return 0;
if(cun%3==0)
{
// cout<<n<<" "<<k<<" "<<d1<<" "<<d2<<endl;
return 1;
}
else return 0;
}
void dos()
{
LL n,k,d1,d2;
cin>>n>>k>>d1>>d2;
LL a,b,c;
a=0;
if(pan(n,k,d1,d2))
{
cout<<"yes"<<endl;
return;
}
if(pan(n,k,-d1,d2))
{
cout<<"yes"<<endl;
return;
}
if(pan(n,k,d1,-d2))
{
cout<<"yes"<<endl;
return;
}
if(pan(n,k,-d1,-d2))
{
cout<<"yes"<<endl;
return;
}
cout<<"no"<<endl;
}
int main()
{
int t;
LL n,k,d1,d2;
while(~scanf("%d",&t))
{
while(t--)
{
dos();
}
}
return 0;
}

D:Count Good Substrings

假如字符串为:abbabbbaaa

我们把这个字符串例如以下记录:

字符串: a b a b a

数组num :1 2 1 3  3

数组的每一项代表这个字符串这个位置的字符是由几个字符压缩成的。

对于回文串的两边都是a的情况:

预处理从起点走奇数步可到达多少a。走偶数步,可到达多少a。

然后从第一个a往后走,可在O(1)的复杂度内得出当前a的为回文串的左边。一共同拥有几个奇数子串。几个偶数子串。

对于回文串的两边都是b的情况,类似与两边都是a的情况。

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<map>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define mem(a,b) (memset(a),b,sizeof(a))
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now int l,int r,int rt
#define INF 99999999
#define LL __int64
#define mod 1000000009
#define eps 1e-6
#define zero(x) (fabs(x)<eps?0:x)
#define maxn 110000
LL a[maxn];
char str[maxn];
vector<char>vec;
int num[maxn];
LL s1,s2;
LL cal(int x,int y)
{
if(x<0)return 0;
LL a1,an,n;
if(y==1)
{
if(x<2)return 0;
an=x-1;
if(x%2)a1=2;
else a1=1;
n=(an-a1)/2+1;
return n*(a1+an)/(LL)2;
}
else
{
an=x;
if(x%2)a1=1;
else a1=2;
n=(an-a1)/2+1;
return n*(a1+an)/(LL)2;
}
}
void dos(int x)
{
int n=vec.size();
LL even,odd;
even=odd=0;
LL sum=0;
for(int i=1; i<=n; i++)
{
if(i%2==x)
{
LL a,b;
a=num[i]/2;
b=num[i]-a;
if(sum%2)swap(a,b);
even+=a;//偶
odd+=b;//奇
}
sum+=num[i];
}
sum=0;
for(int i=x;i<=n;i+=2)
{
LL a,b;
a=num[i]/2;
b=num[i]-a;
if(sum%2)swap(even,odd);
sum=0;
even-=a;
odd-=b;
sum+=num[i];
if(sum%2)swap(even,odd);
sum=0;
s1+=b*odd;
s1+=a*even;
s2+=b*even;
s2+=a*odd;
s1+=cal(num[i],1);
s2+=cal(num[i],2);
sum+=num[i+1];
}
}
int main()
{
int n;
LL s;
while(~scanf("%s",str))
{
vec.clear();
int len=strlen(str);
int s=1;
for(int i=1; i<len; i++)
{
if(str[i]!=str[i-1])
{
vec.push_back(str[i-1]);
num[vec.size()]=s;
s=1;
}
else s++;
}
vec.push_back(str[len-1]);
num[vec.size()]=s;
s1=s2=0;
dos(1);
dos(0);
cout<<s1<<" "<<s2<<endl;
}
return 0;
}

E:Devu and Flowers

做这个题目出了一些莫名其妙的问题。

做法:

假设每个每一种花都有无限个。非常明显,有C(s+n-1,n-1)种取法。

假设某种花取x个以上,那么就有C(s+n-1-x-1,n-1)种取法。

所以就用到容斥:

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
#define maxn 21
#define LL __int64
#define mod 1000000007
LL num[21];
LL inv[21];
void gcd(LL a, LL b, LL& d, LL& x, LL& y) {
if(!b){ d = a; x = 1; y = 0; }
else{ gcd(b, a%b, d, y, x); y -= x*(a/b); }
} LL getInv(LL a, LL n) {
LL d, x, y;
gcd(a, n, d, x, y);
return d == 1 ? ( x + n ) % n : -1;
}
LL com(LL n,LL m)
{ if(n<m)return 0;
LL ans;
ans=1;
for(LL i=n;i>=(n-m+1);i--)
{
ans=ans*(i%mod)%mod;
ans=ans*inv[n-i+1]%mod;
}
return ans;
}
int main()
{
LL n,s;
for(int i=1;i<=20;i++)inv[i]=getInv(i,mod);
while(~scanf("%I64d%I64d",&n,&s))
{
LL r=0;
int m=n;
for(int i=0;i<m;i++)
{
scanf("%I64d",&num[i]);
}
LL ans=com(s+n-1,n-1);
for(int i=1;i<(1<<m);i++)
{
int x=0;
r=0;
for(int j=0;j<m;j++)
{
if(i&(1<<j))
{
x++;
r+=num[j]+1;
}
}
if(x&1)
{
ans-=com(s-r+n-1,n-1);
ans=(ans%mod+mod)%mod;
}
else
{
ans+=com(s-r+n-1,n-1);
ans=(ans%mod+mod)%mod;
}
}
cout<<ans<<endl;
}
return 0;
}

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)的更多相关文章

  1. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  2. Codeforces Round #258 (Div. 2) 小结

    A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...

  3. Codeforces Round #258 (Div. 2) B. Sort the Array

    题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...

  4. Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

    E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...

  5. Codeforces Round #258 (Div. 2) D. Count Good Substrings 水题

    D. Count Good Substrings 题目连接: http://codeforces.com/contest/451/problem/D Description We call a str ...

  6. Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game 水题

    C. Predict Outcome of the Game 题目连接: http://codeforces.com/contest/451/problem/C Description There a ...

  7. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

  8. Codeforces Round #258 (Div. 2) A. Game With Sticks 水题

    A. Game With Sticks 题目连接: http://codeforces.com/contest/451/problem/A Description After winning gold ...

  9. Codeforces Round #258 (Div. 2) 容斥+Lucas

    题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...

  10. Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学

    题目链接:http://codeforces.com/problemset/problem/451/D D. Count Good Substrings time limit per test 2 s ...

随机推荐

  1. Linux下安装matlab2014a

    下载Matlab 我放在百度云盘里了,下载链接: http://pan.baidu.com/s/1pLE1qgr 密码: x4tw 该文件下载解压后如下所示:该文件下载解压后如下所示: 注意linux ...

  2. linux学习笔记-3.文件相关命令

    1.进入到用户根目录 cd ~ 或者 cdcd ~hadoop回到原来路径cd - 2.查看文件详情 stat a.txt 3.移动 mv a.txt /ect/改名mv b.txt a.txt移动并 ...

  3. Bzoj4237 cdq分治+树状数组+单调栈

    二维平面在某区域内点的问题,要么树套树,kdtree,要么就是cdq分治了.然而这题树套树和kdtree都不是很好搞的样子,于是我们就只能cdq分治了.首先把点按照横坐标x排序,在每一层我们需要算出右 ...

  4. BZOJ2905: 背单词 AC自动机+fail树+线段树

    $zjq$神犇一眼看出$AC$自动机 $Orz$ 直接就讲做法了 首先对每个串建出$AC$自动机 将$fail$树找到 然后求出$dfs$序 我们发现一个单词 $S_i$是$S_j$的子串当且仅当$S ...

  5. NOIP 2013 转圈游戏

    [题目描述] n个小伙伴(编号从 0 到 n−1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从 0 到 n−1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……, ...

  6. 堆排序的C++代码实现

    堆排序C++实现 堆排序的具体思路可以查看<算法导论>这本书,一下只提供笔者的C++实现代码,并且将笔者在编写程序的过程当中所遇到的一些细节问题拿出来作一番解释,希望能够对对堆排序有一个透 ...

  7. MikroTik RouterOS使用SATA光驱安装时提示:no CD-ROM found press ENTER to reboot

    可以尝试以下方式: 1.进入bios--Main--Storage configuration,找到SATA configuration ,设置为Compatible模式(即兼容模式.混合模式) 2. ...

  8. STM32 TIMER OUTPUT DIAGRAM

  9. golang-bcc-bpf-function-tracing

    http://www.brendangregg.com/blog/2017-01-31/golang-bcc-bpf-function-tracing.html

  10. 使用React改版网站

    网站是毕业设计的作品,开发这个网站的目的主要用于记录一些笔记,以及聚合一些资讯信息,也算自己在网络世界中的一块静地吧,可以在这里一些技术上想法的实践. 网站最初前端使用vue开发,在前段时间由于项目的 ...