Hdu 2015 Multi-University Training Contest3

来源:互联网 发布:base64加密 js 编辑:程序博客网 时间:2024/05/08 06:54

1002

题面

Problem Description
Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more interesting things about GCD. Today He comes up with Range Greatest Common Divisor Query (RGCDQ). What’s RGCDQ? Please let me explain it to you gradually. For a positive integer x, F(x) indicates the number of kind of prime factor of x. For example F(2)=1. F(10)=2, because 10=2 * 5. F(12)=2, because 12=2 * 2 * 3, there are two kinds of prime factor. For each query, we will get an interval [L, R], Hdu wants to know maxGCD(F(i),F(j)) (L≤ i < j ≤ R)

Input
There are multiple queries. In the first line of the input file there is an integer T indicates the number of queries.
In the next T lines, each line contains L, R which is mentioned above.

All input items are integers.
1<= T <= 1000000
2<=L < R<=1000000

Output
For each query,output the answer in a single line.
See the sample for more details.

Sample Input

2
2 3
3 5

Sample Output

1
1

题意

每次T(1000000)次询问,每次询问有一个区间[L, R] (2 <= L < R <= 1000000 )。
f(i) 表示的是数i的素因子种类数。
然后求这个区间内GCD(f(i), f(j))的最大值,(L <= i < j <= R )。

解析

先暴力打个表,发现这个范围内素因子的种数是不会超过8的。
开一个int cnt[ maxn ] [ 8 ] 的数组来记录在第 i 个数之前(1-7)数字各出现了几次。
然后每次只要暴力这7个数来确定最大的gcd就行了。

代码

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <set>#include <climits>#include <cassert>#include <algorithm>#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int maxn = 1e6 + 1;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = acos(-1.0);const double ee = exp(1.0);int f[maxn];int prime[maxn];bool isPrime[maxn];int nprime;int cnt[maxn][8];void primeTable(){    memset(isPrime, false, sizeof(isPrime));    for(int i = 2; i <= 1000000; ++i)    {        if(!isPrime[i])        {            f[i] = 1;            for(int j = i * 2; j <= 1000000; j += i)            {                isPrime[j] = true;                f[j] += 1;            }        }    }    for(int i = 1; i <= 1000000; ++i)    {        for(int j = 1; j <= 7; ++j)            cnt[i][j] = cnt[i-1][j];        cnt[i][f[i]]++;    }}int gcd(int a, int b){    return b ? gcd(b, a % b) : a;}void solve(){    int ncase;    scanf("%d", &ncase);    while (ncase--)    {        int l, r;        scanf("%d%d", &l, &r);        int tmp[8];        for (int i = 1; i <= 7; i++)            tmp[i] = cnt[r][i] - cnt[l - 1][i];        int ans = 1;        for(int a = 7; a >= 1; a--)            for(int b = 7; b >= 1; b--)            {                if( a == b && tmp[a] >= 2)                {                    ans = max(ans, a);                }                else if(a != b && tmp[a] >= 1 && tmp[b] >= 1)                {                    ans = max(ans, gcd(a, b));                }            }        printf("%d\n", ans);    }}int main(){    primeTable();    solve();    return 0;}

1004

题面

Problem Description
Mr. Hdu is an painter, as we all know, painters need ideas to innovate , one day, he got stuck in rut and the ideas dry up, he took out a drawing board and began to draw casually. Imagine the board is a rectangle, consists of several square grids. He drew diagonally, so there are two kinds of draws, one is like ‘\’ , the other is like ‘/’. In each draw he choose arbitrary number of grids to draw. He always drew the first kind in red color, and drew the other kind in blue color, when a grid is drew by both red and blue, it becomes green. A grid will never be drew by the same color more than one time. Now give you the ultimate state of the board, can you calculate the minimum time of draws to reach this state.

Input
The first line is an integer T describe the number of test cases.
Each test case begins with an integer number n describe the number of rows of the drawing board.
Then n lines of string consist of ‘R’ ‘B’ ‘G’ and ‘.’ of the same length. ‘.’ means the grid has not been drawn.
1<=n<=50
The number of column of the rectangle is also less than 50.

Output
Output an integer as described in the problem description.

Sample Input
2
4
RR.B
.RG.
.BRR
B..R
4
RRBB
RGGB
BGGR
BBRR

Sample Output

3
6

题意

给一个图n*m(注意m!!!!),原来全是点(’.’)。
现在要把图染成输入的样子。
每次操作可以往左刷 ‘\’,一刷到底刷成R,也可以往右刷一刷到底’/’刷成B,如果一个格子被刷2次就被刷成了G,每个格子只能被刷2次。
现在问最少多少次就可以把全是点的图刷成输入的样子。

解析

输入太猥琐了。
只给n,然后队友读题不认真,于是- -。
这题只要模拟一下刷的过程就行了,如果出现了R,就刷R刷到底,出现B就刷B,出现G就左右各刷一次。

代码

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <set>#include <climits>#include <cassert>#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int maxn = 1e6 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = acos(-1.0);const double ee = exp(1.0);char str[60][60];int n, m;void dfsR(int x, int y){    if (0 <= x && x < n && 0 <= y && y < m && (str[x][y] == 'R' || str[x][y] == 'G'))    {        if (str[x][y] == 'G')            str[x][y] = 'B';        else            str[x][y] = '.';        dfsR(x + 1, y + 1);        dfsR(x - 1, y - 1);    }    return;}void dfsB(int x, int y){    if (0 <= x && x < n && 0 <= y && y < m && (str[x][y] == 'B' || str[x][y] == 'G'))    {        if (str[x][y] == 'G')            str[x][y] = 'R';        else            str[x][y] = '.';        dfsB(x - 1, y + 1);        dfsB(x + 1, y - 1);    }    return;}void output(){    cout << "---------" << endl;    for (int i = 0; i <n; i++)    {        cout << str[i] << endl;    }    cout <<endl;}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);#endif // LOCAL    int ncase;    scanf("%d", &ncase);    while (ncase--)    {        scanf("%d", &n);        for (int i = 0; i < n; i++)            scanf("%s", str[i]);        m = strlen(str[0]);        int ans = 0;        for (int i = 0; i < n; i++)        {            for (int j = 0; j < m; j++)            {                if (str[i][j] == 'G')                {                    dfsR(i, j);                    str[i][j] = 'B';                    dfsB(i, j);                    ans += 2;                }                else if (str[i][j] == 'R')                {                    dfsR(i, j);                    ans++;//                    output();                }                else if (str[i][j] == 'B')                {                    dfsB(i, j);                    ans++;//                    output();                }            }        }        printf("%d\n", ans);    }    return 0;}

1008

题面

Problem Description
Have you learned something about segment tree? If not, don’t worry, I will explain it for you.
Segment Tree is a kind of binary tree, it can be defined as this:
- For each node u in Segment Tree, u has two values: Lu and Ru.
- If Lu=Ru, u is a leaf node.
- If Lu≠Ru, u has two children x and y, with Lx=Lu,Rx=⌊Lu+Ru2⌋,Ly=⌊Lu+Ru2⌋+1,Ry=Ru.
Here is an example of segment tree to do range query of sum.
这里写图片描述
Given two integers L and R, Your task is to find the minimum non-negative n satisfy that: A Segment Tree with root node’s value Lroot=0 and Rroot=n contains a node u with Lu=L and Ru=R.

Input
The input consists of several test cases.
Each test case contains two integers L and R, as described above.
0≤L≤R≤109
LR−L+1≤2015

Output
For each test, output one line contains one integer. If there is no such n, just output -1.

Sample Input

6 7
10 13
10 11

Sample Output

7
-1
12

题意

给一棵线段树访问的范围[L, R],然后求计算中能覆盖到这个范围的[0,rt]中最小的rt是多少。

解析

这题也是无脑暴,比赛的时候学弟推出了递推的式子,但是没有用上。
主要在于两个剪枝和递推的顺序,不改顺序会暴栈,要先递归L那边。
递归式子:
[L, R] ->
[2 * L - R - 1, R]
[2 * L - R - 2, R]
[L, 2 * R - L + 1]
[L, 2 * R - L]
主要由线段树的性质推出这四种情况,然后dfs暴它。

代码

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <set>#include <climits>#include <cassert>#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int maxn = 1e6 + 10;const LL inf = 0x3f3f3f3f;LL ans;void dfs(LL L, LL R){    if (L == 0)    {        if (R < ans)            ans = R;        return;    }    if (L - 1 < R - L)        return;    dfs(L - 1 - (R - L), R);    dfs(L - 1 - (R - L) - 1, R);    if (R + 1 + R - L >= ans)        return;    dfs(L, R + 1 + R - L);    dfs(L, R + 1 + R - L - 1);}int main(){    #ifdef LOCAL    freopen("in.txt", "r", stdin);    #endif // LOCAL    LL l, r;    while (scanf("%I64d%I64d", &l, &r) != EOF)    {        ans = inf;        dfs(l, r);        if (ans == inf)            printf("-1\n");        else            printf("%I64d\n", ans);    }    return 0;}

1011

题面

Problem Description
It’s an interesting experience to move from ICPC to work, end my college life and start a brand new journey in company.
As is known to all, every stuff in a company has a title, everyone except the boss has a direct leader, and all the relationship forms a tree. If A’s title is higher than B(A is the direct or indirect leader of B), we call it A manages B.
Now, give you the relation of a company, can you calculate how many people manage k people.

Input
There are multiple test cases.
Each test case begins with two integers n and k, n indicates the number of stuff of the company.
Each of the following n-1 lines has two integers A and B, means A is the direct leader of B.

1 <= n <= 100 , 0 <= k < n
1 <= A, B <= n

Output
For each test case, output the answer as described above.

Sample Input

7 2
1 2
1 3
2 4
2 5
3 6
3 7

Sample Output

2

题意

给一张有向图n个点,n - 1(。。。。输入n-1)条边。
A指向B代表A管理B,然后可以间接管理,比如A管理B,B管理C,则A管理C。
现在问管理k个人的人有多少个。

解析

大水题,就因为输入n-1坑了两发。T T
n比较小,怎么搞都可以吧。
先用floyd把间接相连的边连起来,别加i!=j之类的。。。
然后再暴力就行了。

代码

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <set>#include <climits>#include <cassert>#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int maxn = 100 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = acos(-1.0);const double ee = exp(1.0);int g[maxn][maxn];int n, k;void floyd(){    for (int p = 1; p <= n; p++)    {        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= n; j++)            {                if(g[i][p] && g[p][j])                {                    g[i][j] = 1;                }            }        }    }}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);#endif // LOCAL    while (~scanf("%d%d", &n, &k))    {        memset(g, 0, sizeof(g));        for (int i = 0; i < n - 1; i++)        {            int fr, to;            scanf("%d%d", &fr, &to);            g[fr][to] = 1;        }        floyd();        int ans = 0;        for (int i = 1; i <= n; i++)        {            int cnt = 0;            for (int j = 1; j <= n; j++)            {                if (i != j)                {                    if (g[i][j])                    {                        cnt++;                    }                }            }            if (cnt == k)            {                ans++;            }        }        printf("%d\n", ans);    }    return 0;}

0 0
原创粉丝点击