Codeforces Round #411 (Div. 2)

链接:

http://codeforces.com/contest/805

A

大意是:给出两个整数 i, j。输出的数满足这样的条件,它能被尽可能多的从 i 到 j 区间的数整除。
思路:只要 i, j 不相等,那么2肯定是能被区间的数整除最多的。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int main() {

    int i, j;
    scanf("%d%d", &i, &j);
    if (i == j) printf("%d\n", i);
    else
        printf("2\n");
    return 0;
}

大意是:用a,b,c三个字母组成一个n位的字符串。要求是:每个三位的子串不能是回文数,c尽可能的少。如果有多组答案,输出任意一组即可。

思路:只要是第i个字母不等于第i+2个字母,而且不用字母c就可以。比如abba循环。

以下是代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include "stdio.h"
#include <stdio.h>
using namespace std;
int main() {

    int n;
    scanf("%d", &n);
    int num = n / 4;
    for (int i = 1; i <= num; i++) {
        printf("abba");
    }
    if (n % 4 == 1)
        printf("a");
    else if (n % 4 == 2)
        printf("ab");
    else if (n % 4 == 3)
        printf("abb");
    else
        printf("");

    printf("\n");
    return 0;
}

C

大意:有n个学校,有个人要从某个学校出发,遍历所有学校,求最小的花费。其中,第 i 个学校和第 j 个学校之间的花费是 (i+j)mod(n+1)。

#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include "stdio.h"
#include <stdio.h>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    int ans = (n + 1) / 2 - 1;
    printf("%d\n", ans);
    return 0;
}

D

大意:有一个字符串仅由a,b组成,长度不超过1e6。现在要将所有的ab子串都换成bba,求转换所有的ab的最小的步数。因为数比较大,所以对结果取模1e9+7。

思路:仔细分析之后,会发现,其实最后所有的a都会被移到最后面去。当要移动x个a时,需要的步数是2^x-1,而且移动完前面的x个a,当后面再发现a时,乘方是需要累加的。找到这个规律之后,编程就容易多了。
但是因为指数可能会达到1e6,所以直接算肯定会超时,需要用到快速幂来做。
我在快速幂的时候因为有几个变量没有取模,导致错误,一定要注意:所有可能爆的地方都要取模。
代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include "stdio.h"
#include <stdio.h>
using namespace std;
#define modd 1000000007
char s[1000005];

long long pow(int base, int n) {
    long long ans = 1;
    long long multi = base;
    long long temp = n;
    while (temp > 0) {
        if (temp % 2 == 1)
            ans = ans * multi;
        ans = ans % modd;
        multi = (multi * multi)%modd;//因为这里没有取模,卡了好几次
        temp /= 2;
    }
    return ans;
}

int main() {
    scanf("%s", s);
    long long ans = 0;
    int temp = 0;
    int length = strlen(s);
    for (int i = 0; i < length; i++) {
        if (s[i] == 'a') {
            temp++;
        }
        else { //开始没有用快速幂,超时了
            long long temp2 = pow(2, temp);
            ans = (ans + (temp2 - 1)%modd) % modd;
        }

    }
    printf("%I64d\n", ans);//注意,输出longlong的时候用I64d

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值