Vjudge 2016-5-10 math test

刚刚讲完数论,善良的学长就为我们举办了一次紧张刺激的考(zuo)试(ti)。
A题
- A Simple Problem with Integers

Description
给出了一个序列,你需要处理如下两种询问。
“C a b c”表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。
“Q a b” 询问[a, b]区间中所有值的和。

Input
第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.
第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。
接下来Q行询问,格式如题目描述。

Output
对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。

Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output
4
55
9
15

鉴于刚刚学完线段树,A题就出了一道线段树裸题来复习……

#include <iostream>
#include <cstdio> 
using namespace std;
typedef long long LL;
const int SZ = 2000000;

struct sgt
{
    int l, r;
    LL sum, add;
}tree[SZ];
int num[SZ];
char op[5];

void update(int now)
{
    tree[now].sum = tree[now << 1].sum + tree[now << 1 | 1].sum;
}

void build(int now, int l, int r)
{
    tree[now].l = l;
    tree[now].r = r;
    if(l == r)
    {
        tree[now].sum = num[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(now << 1, l, mid);
    build(now << 1 | 1, mid + 1, r);
    update(now); 
}

void pushdown(int now)
{
    if(tree[now].add != 0)
    {
        tree[now << 1].sum += (tree[now << 1].r - tree[now << 1].l + 1) * tree[now].add;
        tree[now << 1].add += tree[now].add;
        tree[now << 1 | 1].sum += (tree[now << 1 | 1].r - tree[now << 1 | 1].l + 1) * tree[now].add;
        tree[now << 1 | 1].add += tree[now].add;
        tree[now].add = 0;
        update(now);
    }
}

void change(int now, int l, int  r, int value)
{
    if(l <= tree[now].l && tree[now].r <= r)
    {
        tree[now].sum += (tree[now].r - tree[now].l + 1) * value;
        tree[now].add += value;
        return ;
    }
    pushdown(now);
    int mid = (tree[now].l + tree[now].r) >> 1;
    if(l <= mid)
        change(now << 1, l, r, value);
    if(r > mid)
        change(now << 1 | 1, l, r, value);
    update(now);
}

LL ask(int now, int l, int r)
{
    if(l <= tree[now].l && tree[now].r <= r)
        return tree[now].sum;
    pushdown(now);
    LL ans = 0;
    int mid = (tree[now].l + tree[now].r) >> 1;
    if(l <= mid)
        ans += ask(now << 1, l, r);
    if(r > mid)
        ans += ask(now << 1 | 1, l, r);
    return ans;
}

int main()
{
    int n, q;
    scanf("%d%d", &n, &q);
    for(int i = 1;i <= n; i ++)
        scanf("%d", &num[i]);
    build(1, 1, n);
    while(q --)
    {
        int l, r, value;
        scanf("%s", op);
        if(op[0] == 'Q')
        {
            scanf("%d%d", &l, &r);
            printf("%lld\n", ask(1, l, r));
        }
        if(op[0] == 'C')
        {
            scanf("%d%d%d", &l, &r, &value);
            change(1, l, r, value);
        }
    } 
    return 0;
}

B题
- Fibonacci

Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is

这里写图片描述

Given an integer n, your goal is to compute the last 4 digits of Fn.

Input
The input test file will contain multiple test cases. Each test case
consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000).
The end-of-file is denoted by a single line containing the number −1.

Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

Sample Input
0
9
999999999
1000000000
-1

Sample Output
0
34
626
6875

Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
这里写图片描述
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
引用块内容

如学长们所说,这个题的答案就写在题面上……
矩阵 + 快速幂求斐波那契数列……

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
const int k = 10000;

struct mat
{
    LL a[3][3];
};
mat operator *(mat a, mat b)
{
    mat c;
    for(int i = 1; i <= 2; i++)
    for(int j = 1; j <= 2; j++)
        c.a[i][j] = ((a.a[i][1] * b.a[1][j]) % k + (a.a[i][2] * b.a[2][j]) % k) % k;
    return c;
}
mat s;

mat ksm(mat x,int y)
{
    if(y == 1)
        return x;
    mat a = ksm(x, y/2);
    if(y % 2)
        return a * a * s;
    else
        return a * a;
}

int main()
{
    s.a[1][1] = 1;
    s.a[1][2] = 1;
    s.a[2][1] = 1;
    s.a[2][2] = 0;
    int n;
    mat q;
    q.a[1][1] = 1;
    q.a[1][2] = 0;
    q.a[2][1] = 0;
    q.a[2][2] = 0;
    scanf("%d", &n);
    while(n != -1)
    {
        if(n == 0)
            printf("0\n");
        if(n == 1|| n == 2)
            printf("1\n");
        if(n > 2)
        {
            mat ans = q * ksm(s, n-1);
            printf("%d\n", ans.a[1][1]);
        }
        scanf("%d", &n);
    } 
}

C题
- 青蛙的约会

Description
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。

Input
输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。

Output
输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行”Impossible”

Sample Input
1 2 3 4 5

Sample Output
4

拓展欧几里得求解……

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
LL x, y, m, n, l;

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(b == 0)
    {
        x = 1; y = 0;
        return a;
    }
    LL t = exgcd(b, a % b, x, y);
    LL xx = x;
    x = y;
    y = xx - a / b * y;
    return t; 
}

int main()
{
    scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l);
    if(m == n)
        printf("Impossible");
    else
    {
        if(m < n)   swap(m, n), swap(x, y);
        LL a, k;
        LL c = y - x;

        LL d = exgcd(m-n, l, a, k);
        if(c % d)
            printf("Impossible");
        else
        {
            LL ans = (((a * c / d) % (l / d) + (l /d)) % (l /d));
            printf("%lld", ans);
        }
    }
    return 0;
}

D题
-Help is needed for Dexter

Dexter is tired of Dee Dee. So he decided to keep Dee Dee busy in a game. The game he planned for her is quite easy to play but not easy to win at least not for Dee Dee. But Dexter does not have time to spend on this silly task, so he wants your help.
There will be a button, when it will be pushed a random number N will be chosen by computer.Then on screen there will be numbers from 1 to N. Dee Dee can choose any number of numbers from the numbers on the screen, and then she will command computer to subtract a positive number chosen by her (not necessarily on screen) from the selected numbers. Her objective will be to make all the numbers 0.
For example if N = 3, then on screen there will be 3 numbers on screen: 1, 2, 3. Say she now selects 1 and 2. Commands to subtract 1, then the numbers on the screen will be: 0, 1, 3. Then she selects 1 and 3 and commands to subtract 1. Now the numbers are 0, 0, 2. Now she subtracts 2 from 2 and all the numbers become 0.
Dexter is not so dumb to understand that this can be done very easily, so to make a twist he will give a limit L for each N and surely L will be as minimum as possible so that it is still possible to win
within L moves. But Dexter does not have time to think how to determine L for each N, so he asks you to write a code which will take N as input and give L as output.

Input
Input consists of several lines each with N such that 1 ≤ N ≤ 1, 000,000, 000. Input will be terminated by end of file

Output
For each N output L in separate lines.

Sample Input
1
2
3

Sample Output
1
2
2

题目的大体意思是给定数 1 - n,每次选择一个区间减去同一个数,不能小于0。最后输出n行,第i行表示把i变为0的最少操作数。
一个找规律的题……

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;

LL ask(LL n)
{
    return (n == 1) ? 1 : ask(n / 2) + 1;
}

int main()
{
    LL n, haha = 0;
    while(scanf("%lld", &n) != EOF)
    {
        printf("%lld\n", ask(n));
    }
    return 0;
}

E题
- 水题

Description
监狱有连续编号为1…N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱

Input
输入两个整数M,N.1<=M<=10^8,1<=N<=10^12

Output
可能越狱的状态数,模100003取余

Sample Input
2 3

Sample Output
6

Hint
6种状态为(000)(001)(011)(100)(110)(111)

要推公式,好麻烦的……看代码就可以明白吧……

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;

LL ksm(LL m, LL n)
{
    if(m == 1)  return 1;
    if(m == 0)  return 0;
    if(n == 0)  return 1;
    if(n == 1)  return m;
    LL haha = ksm(m, n / 2);
    if(n % 2 == 0)
        return ((haha % 100003) * (haha % 100003)) % 100003;
    else
        return ((haha % 100003) * (haha % 100003) * (m % 100003)) % 100003;
}

int main()
{
    LL n, m;
    scanf("%lld%lld", &m, &n);
    LL tot = ksm(m, n);
    LL haha = ((m % 100003) * (ksm(m- 1, n -1) % 100003)) % 100003;
    LL ans;
    if(tot < haha)
        ans = ((tot + 100003) - haha) % 100003;
    else
        ans = ((tot % 100003) - (haha % 100003)) % 100003;
    printf("%lld", ans);
    return 0;
}

嗯,这样愉快的math test 就结束了……题目难度还可以吧……愉快的拿到了rank 1……

posted @ 2016-05-14 20:49  Loi_Vampire  阅读(134)  评论(0编辑  收藏  举报