比赛题解 (3)—— 思维

CodeForces 1017B

Description

Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question:

Given two binary numbers aa and bb of length nn. How many different ways of swapping two digits in aa (only in aa, not bb) so that bitwise OR of these two numbers will be changed? In other words, let cc be the bitwise OR of aa and bb, you need to find the number of ways of swapping two bits in aa so that bitwise OR will not be equal to cc.

Note that binary numbers can contain leading zeros so that length of each number is exactly nn.

Bitwise OR is a binary operation. A result is a binary number which contains a one in each digit if there is a one in at least one of the two numbers. For example, 01010 (B)  OR 10011 (B) = 11011 (B)

Well, to your surprise, you are not Rudolf, and you don't need to help him…… You are the security staff! Please find the number of ways of swapping two bits in aa so that bitwise OR will be changed.

Input

The first line contains one integer nn (2≤n≤1052≤n≤105) — the number of bits in each number.

The second line contains a binary number aa of length nn.

The third line contains a binary number bb of length nn.

Output

Print the number of ways to swap two bits in aa so that bitwise OR will be changed.

Sample Input

Input

5
01011
11001

Output

4

Input

6
011000
010011

Output

6

Hint

In the first sample, you can swap bits that have indexes (1,4)(1,4), (2,3)(2,3), (3,4)(3,4), and (3,5)(3,5).

In the second example, you can swap bits that have indexes (1,2)(1,2), (1,3)(1,3), (2,4)(2,4), (3,4)(3,4), (3,5)(3,5), and (3,6)(3,6).

题意:

给定二进制数 a,b ,a 或 b 的结果是 c

现在来交换 a 中的某两位数,使得 与 b 或完之后的二进制数 不等于 c

OR :

是  或 ( | ) 的意思,如果两位中有一个是 1 ,那么或 完之后的结果就是 1

XOR:

是  异或 (^)  的意思,如果两位相同,结果为 0,不同 为 1

 

分析:

如果交换的两位数 在 b 中是 1的话,a 相应位置不论怎么交换,值都不会改变

所以就去找 b 中 为 0 的数

讨论出现的情况:

(1) a:0  ————  1           1     (先找到 b 为 0,那么 a 可以是 1或者是 0)

          b:0  ————  0           1     (当 a 是 0的时候,必须找 a 中 是 1的位置交换,这样才有可能改变 c 的值                                                                                               b 的值 可以取 0或者是 1)

(2) a:1  ————  0           0

          b:0  ————  0           1

转化成 求 sum(0,0)*sum(1,0)+sum(0,0)*sum(1,1)+sum(1,0)*sum(0,1)

每一种情况出现的次数相乘 总和 就是总的方法数

 

CODE: 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define memset(a,n) memset(a,n,sizeof(a))
#define maxx 100000+10
int main()
{
    LL n;
    LL ans=0;
    char s[maxx];
    char c[maxx];
    scanf("%lld",&n);
    cin>>s;
    cin>>c;
    LL x,y,a,b;
    for(LL i=0;i<n;i++)
    {
        if(s[i]=='0'&&c[i]=='0')
            a++;
        if(s[i]=='1'&&c[i]=='0')
            b++;
        if(s[i]=='1'&&c[i]=='1')
            x++;
        if(s[i]=='0'&&c[i]=='1')
            y++;
    }
   // cout<<a<<' '<<b<<' '<<x<<' '<<y<<endl;
    ans=a*b+a*x+b*y;
    printf("%lld\n",ans);
}

 


CodeForces 1016A

 

Description

You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: "You have to write names in this notebook during nn consecutive days. During the ii-th day you have to write exactly aiai names.". You got scared (of course you got scared, who wouldn't get scared if he just receive a notebook which is named Death Note with a some strange rule written in it?).

Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly mm names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn't matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.

Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from 11 to nn.

Input

The first line of the input contains two integers nn, mm (1≤n≤2⋅1051≤n≤2⋅105, 1≤m≤1091≤m≤109) — the number of days you will write names in the notebook and the number of names which can be written on each page of the notebook.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai means the number of names you will write in the notebook during the ii-th day.

Output

Print exactly nn integers t1,t2,…,tnt1,t2,…,tn, where titi is the number of times you will turn the page during the ii-th day.

Sample Input

Input

3 5
3 7 9

Output

0 2 1 

Input

4 20
10 9 19 2

Output

0 0 1 1 

Input

1 100
99

Output

0 

Hint

In the first example pages of the Death Note will look like this [1,1,1,2,2],[2,2,2,2,2],[3,3,3,3,3],[3,3,3,3][1,1,1,2,2],[2,2,2,2,2],[3,3,3,3,3],[3,3,3,3]. Each number of the array describes during which day name on the corresponding position will be written. It is easy to see that you should turn the first and the second page during the second day and the third page during the third day.

 

题意:

每天需要写相应个数的 名字 a [ i ],每张纸需要写满 m 个名字才能翻页

求每一天  t [ i ] 的翻页数

思路:

用 前缀和的思想来做

 首先  a [ i ] /m 是 当天翻页的数量, 如果  a [ i ] 能够到区间长度的倍数就输出并清零,否则一直累加

CODE

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define memset(a,n) memset(a,n,sizeof(a))
#define maxx 200000+10
LL a[maxx];
LL ans[maxx];

int main()
{
    LL n,m;
    scanf("%lld%lld",&n,&m);
    memset(a,0);
    for(LL i=1; i<=n; i++)
        scanf("%lld",&a[i]);
    LL sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=a[i];
        ans[i]=sum/m;
        sum=sum-ans[i]*m;
    }
    for(LL i=1; i<=n; i++)
    {
        if(i!=1)
            printf(" ");
        printf("%lld",ans[i]);
    }
}

CodeForces 1004B

Description

Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.

There are nn flowers in a row in the exhibition. Sonya can put either a rose or a lily in the ii-th position. Thus each of nn positions should contain exactly one flower: a rose or a lily.

She knows that exactly mm people will visit this exhibition. The ii-th visitor will visit all flowers from lili to riri inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.

Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.

Input

The first line contains two integers nn and mm (1≤n,m≤1031≤n,m≤103) — the number of flowers and visitors respectively.

Each of the next m lines contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n), meaning that ii-th visitor will visit all flowers from lili to riri inclusive.

Output

Print the string of nn characters. The ii-th symbol should be «0» if you want to put a rose in the ii-th position, otherwise «1» if you want to put a lily.

If there are multiple answers, print any.

Sample Input

Input

5 3
1 3
2 4
2 5

Output

01100

Input

6 3
5 6
1 4
4 6

Output

110010

Hint

In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions;

  • in the segment [1…3][1…3], there are one rose and two lilies, so the beauty is equal to 1⋅2=21⋅2=2;
  • in the segment [2…4][2…4], there are one rose and two lilies, so the beauty is equal to 1⋅2=21⋅2=2;
  • in the segment [2…5][2…5], there are two roses and two lilies, so the beauty is equal to 2⋅2=42⋅2=4.

The total beauty is equal to 2+2+4=82+2+4=8.

In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions;

  • in the segment [5…6][5…6], there are one rose and one lily, so the beauty is equal to 1⋅1=11⋅1=1;
  • in the segment [1…4][1…4], there are two roses and two lilies, so the beauty is equal to 2⋅2=42⋅2=4;
  • in the segment [4…6][4…6], there are two roses and one lily, so the beauty is equal to 2⋅1=22⋅1=2.

The total beauty is equal to 1+4+2=71+4+2=7.

题意:

给定区间有玫瑰花和百合花,该区间的值= 玫瑰花的个数  × 百合花的个数

若求得的值最大的话,应该保证 玫瑰和百合 都有,并且要均分

例如: 4: 2*2  > 3*1

所以不能出先连续的玫瑰或者百合,这样有可能再某个区间出现 百合或者玫瑰个数为 0 的情况,这样值就是 0

思路:

直接输出 01010101......  就好了,

与输入无关,也是让人很无语的题目了

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define memset(a,n) memset(a,n,sizeof(a))
#define maxx 100000+10

int main()
{
    int n,q;
    int a[1001],b[1001];
    scanf("%d%d",&n,&q);
    for(int i=0;i<q;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
    }
    for(int i=0;i<n;i++)
    {
        if(i%2==0)
            printf("0");
        else
            printf("1");
    }
    printf("\n");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值