Codeforces Round #330 (Div. 2) (595A,595B(容斥原理))

Vitaly and Night

题目链接:

http://codeforces.com/problemset/problem/595/A

解题思路:

It was easy realization problem. Let's increase the variable i from 1 to n, and inside let's increase the variable j from 1 to m. On every iteration we will increase the variable j on 2. If on current iteration a[i][j] = '1' or a[i][j + 1] = '1' let's increase the answer on one.

Asymptotic behavior of this solution — O(nm).

题目大意:

有n层楼, 每层有2 * m个窗户, 每家有2个窗户, 任何一个窗户亮说明这家还未休息, 问你有几家没有休息。

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        int x,cnt = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                int flag = 0;
                for(int k = 0; k < 2; k++){
                    scanf("%d",&x);
                    if(x == 1)
                        flag = 1;
                }
                if(flag)
                    cnt++;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

Pasha and Phone

题目链接:

http://codeforces.com/problemset/problem/595/B

解题思路:

Let's calculate the answer to every block separately from each other and multiply the answer to the previous blocks to the answer for current block.

For the block with length equals to k we can calculate the answer in the following way. Let for this block the number must be divided on xand must not starts with digit y. Then the answer for this block — the number of numbers containing exactly k digits and which divisible byx, subtract the number of numbers which have the first digit equals to y and containing exactly k digits and plus the number of numbers which have the first digit equals to y - 1 (only if y > 0) and containing exactly k digits.

Asymptotic behavior of this solution — O(n / k).

题目大意:

给出你n / k个a[i]和n / k个b[i], 问你有多少个k位数, 使得不以b[i]开头并且可以整除a[i]。

算法思想:

数学题,,容斥定理。 求出所有情况的总数, 减去以b[i]开头的数目就为答案。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long ll;
const ll mod = 1e9 + 7;
ll num[15];
ll a[100005],b;

int main(){
    num[0] = 1;
    for(int i = 1; i <= 10; i++)
        num[i] = num[i-1]*10;
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        int sum = n/k;
        for(int i = 0; i < sum; i++)
            scanf("%lld",&a[i]);
        ll ans = 1;
        for(int i = 0; i < sum; i++){
            scanf("%lld",&b);
            ll cnt = (num[k]-1)/a[i]+1;
            ll l = b*num[k-1],r = (b+1)*num[k-1]-1;
            if(l == 0)
                cnt = cnt-(r/a[i]+1);
            else
                cnt = cnt -(r/a[i]-(l-1)/a[i]);
            ans = (ans*cnt)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值