【 题集 】 T_T ac - contest - 9

    12月份的第一篇博文,很久没敲代码了,毕竟是弱菜一枚,我也就不多说什么了, 得过且过的大二,悔、、!

    A - Arcane Numbers 1

   

Description

Vance and Shackler like playing games. One day, they are playing a game called "arcane numbers". The game is pretty simple, Vance writes down a finite decimal under base A, and then Shackler translates it under base B. If Shackler can translate it into a finite decimal, he wins, else it will be Vance’s win. Now given A and B, please help Vance to determine whether he will win or not. Note that they are playing this game using a mystery language so that A and B may be up to 10^12.
 

Input

The first line contains a single integer T, the number of test cases.
For each case, there’s a single line contains A and B.
 

Output

For each case, output “NO” if Vance will win the game. Otherwise, print “YES”. See Sample Output for more details.
 

Sample Input

    
    
3 5 5 2 3 1000 2000
 

Sample Output

  
  
Case #1: YES Case #2: NO Case #3: YES

这题就是要求给定的两个数,问A 里面的因子,是否在B 里都有,可以说是gcd 的模版题了吧、

   

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;

typedef __int64 LL;

LL gcd(LL a, LL b)
{
    LL t;
    if(a < b)
    {
        t = a;
        a = b;
        b = t;
    }
    LL r;
    r = a % b;
    while(r)
    {
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

int main()
{
    int t;
    LL a, b, tmp;
    while(~scanf("%d",&t))
    {
        int icase = 1;
        while(t --)
        {
            scanf("%I64d%I64d", &a, &b);
            while(a != 1)
            {
                tmp = gcd(a, b);
                if(tmp == 1)
                {
                    break;
                }
                a = a / tmp;
            }
            printf("Case #%d: ", icase ++);
            if(a == 1)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
}

    B - Candy

   

Description

There are N candies and M kids, the teacher will give this N candies to the M kids. The i-th kids for the j-th candy has a preference for like[i][j], if he like the sugar, like[i][j] = 1, otherwise like[i][j] = 0. If the i-th kids get the candy which he like he will get K glad value. If he or she do not like it. He will get only one glad value. We know that the i-th kids will feel happy if he the sum of glad values is equal or greater than B[i]. Can you tell me whether reasonable allocate this N candies, make every kid feel happy.
 

Input

The Input consists of several cases .The first line contains a single integer t .the number of test cases.
For each case starts with a line containing three integers N, M, K (1<=N<=13, 1<=M<=13, 2<=K<=10)
The next line contains M numbers which is B[i](0<=B[i]<=1000). Separated by a single space.
Then there are M*N like[i][j] , if the i-th kids like the j-th sugar like[i][j]=1 ,or like[i][j]=0.
 

Output

If there have a reasonable allocate make every kid feel happy, output "YES", or "NO".
 

Sample Input

    
    
2 3 2 2 2 2 0 0 0 0 0 1 3 2 2 2 2 0 0 0 0 0 0
 

Sample Output

    
    
Case #1: YES Case #2: NO

Hint

 Give the first and second candy to the first kid. Give the third candy to the second kid. This allocate make all kids happy. 

   
 这题题意简单,但是我用的是模拟+暴力,不知道为何过不了,因为写的比较复杂,也不想再去debug了,先mark 一下,以后再来看看,据说是网络流、、

    (错误的代码!)

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int like[15][15];
int b[15];
int tt[15];

int t;
int n, m, k;

int main()
{
    while(~scanf("%d",&t))
    {
           int icase = 1;
            while(t --)
            {
                memset(like, 0, sizeof(like));
                memset(tt, 0, sizeof(tt));
                scanf("%d%d%d",&n, &m, &k);
                for(int i = 0; i < m; i ++)
                    scanf("%d",&b[i]);
                for(int i = 0; i < m; i ++)
                {
                    for(int j = 0; j < n; j ++)
                    {
                        scanf("%d",&like[i][j]);
                        if(like[i][j] == 1)
                            tt[i] ++;
                    }
                }
                int cnt = 0;
                int tmp = n;
                for(int i = 0; i < m; i ++)
                {
                    int ok = 0;
                    if(tmp < 0)
                        break;
                    if(tt[i] * k >= b[i])
                    {
                        for(int j = 0; j <= tt[i]; j ++)
                        {
                            if(j * k >= b[i])
                            {
                                 if(tmp - j >= 0)
                                 {
                                     cnt ++;
                                     tmp = tmp - j;
                                     ok = 1;
                                     break;
                                 }
                            }
                            if(ok)
                                break;
                        }
                    }
                    else
                    {
                        int cc = tt[i] * k;
                        if(tmp - b[i] - cc >= 0)
                        {
                            tmp = tmp - b[i] - cc;
                            cnt ++;
                        }
                    }

                }
                //printf("%d\n", cnt);
                if(cnt == m)
                    printf("Case #%d: YES\n", icase ++);
                else
                    printf("Case #%d: NO\n", icase ++);

            }
    }
}

    D - Triangle LOVE

Description

Recently, scientists find that there is love between any of two people. For example, between A and B, if A don’t love B, then B must love A, vice versa. And there is no possibility that two people love each other, what a crazy world!
Now, scientists want to know whether or not there is a “Triangle Love” among N people. “Triangle Love” means that among any three people (A,B and C) , A loves B, B loves C and C loves A.
  Your problem is writing a program to read the relationship among N people firstly, and return whether or not there is a “Triangle Love”.
 

Input

The first line contains a single integer t (1 <= t <= 15), the number of test cases.
For each case, the first line contains one integer N (0 < N <= 2000).
In the next N lines contain the adjacency matrix A of the relationship (without spaces). A i,j = 1 means i-th people loves j-th people, otherwise A i,j = 0.
It is guaranteed that the given relationship is a tournament, that is, A i,i= 0, A i,j ≠ A j,i(1<=i, j<=n,i≠j).
 

Output

For each case, output the case number as shown and then print “Yes”, if there is a “Triangle Love” among these N people, otherwise print “No”.
Take the sample output for more details.
 

Sample Input

    
    
2 5 00100 10000 01001 11101 11000 5 01111 00000 01000 01100 01110
 

Sample Output

    
    
Case #1: Yes Case #2: No
 

    可以用搜索A 掉它, 但是看题解是 用拓扑的思路、、、

    我写的是错的,都不好意思贴出来了 - -#

    E - Flowers

Description

As is known to all, the blooming time and duration varies between different kinds of flowers. Now there is a garden planted full of flowers. The gardener wants to know how many flowers will bloom in the garden in a specific time. But there are too many flowers in the garden, so he wants you to help him.
 

Input

The first line contains a single integer t (1 <= t <= 10), the number of test cases.
For each case, the first line contains two integer N and M, where N (1 <= N <= 10^5) is the number of flowers, and M (1 <= M <= 10^5) is the query times.
In the next N lines, each line contains two integer S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, means the time of i-th query.
 

Output

For each case, output the case number as shown and then print M lines. Each line contains an integer, meaning the number of blooming flowers.
Sample outputs are available for more details.
 

Sample Input

    
    
2 1 1 5 10 4 2 3 1 4 4 8 1 4 6
 

Sample Output

  
  
Case #1: 0 Case #2: 1 2 1

    线段树 ||  树状数组  + 离散化。

    这边稍微提一下,函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置。unique的函数是去除相邻的重复元素(只保留一个)。其返回值为地址,unique(num, num + n) - num 表示不重复的个数。

   

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

#define N 100010

int sum[N];

typedef struct node
{
    int s, e;
}Node;

Node tt[N];
int a[N * 4];
int flo[N];
int n, m;

int lowbit(int x)
{
    return x & (-x);
}

void add(int i, int x, int f)
{
    while(i <= f)
    {
        sum[i] += x;
        i += lowbit(i);
    }
}

int getsum(int x)
{
    int cnt = 0;
    while(x > 0)
    {
        cnt += sum[x];
        x -= lowbit(x);
    }
    return cnt;
}

int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        int icase = 1;
        while(t --)
        {
            int cot = 0;
            scanf("%d%d",&n,&m);
            memset(sum, 0, sizeof(sum));
            for(int i = 0; i < n; i ++)
            {
                scanf("%d%d", &tt[i].s , &tt[i].e);
                a[cot ++] = tt[i].s;
                a[cot ++] = tt[i].e;
            }

            for(int i = 0; i < m; i ++)
            {
                scanf("%d", &flo[i]);
                a[cot ++] = flo[i];
            }
            sort (a, a + cot);
            cot = unique(a, a + cot) - a;
            for(int i = 0;i < n; i ++)
            {
                int tmp1 = lower_bound(a, a + cot, tt[i].s) - a + 1;
                int tmp2 = lower_bound(a, a + cot, tt[i].e) - a + 1;
                add(tmp1, 1, cot);
                add(tmp2 + 1, -1, cot);
            }
            printf("Case #%d:\n", icase ++);
            for(int i = 0; i < m; i ++)
            {
                int ans = lower_bound(a, a + cot, flo[i]) - a + 1;
                printf("%d\n", getsum(ans));
            }
        }

    }
}

    F - Binary Tree Traversals

   

Description

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
 

Input

The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
 

Output

For each test case print a single line specifying the corresponding postorder sequence.
 

Sample Input

    
    
9 1 2 4 7 3 5 8 9 6 4 7 2 1 8 5 9 3 6
 

Sample Output

  
  
7 4 2 8 9 5 6 3 1

    该题就是给你中序遍历和后序遍历二叉树的结果,让你求出先序遍历的结果。主要是用到递归,搞的我好久,最后还是在别人的帮忙下,搞定的。当然,你也可以自己想想知道三个中的任意两个,求另外一个的思路、

   

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

typedef struct tree
{
    tree *l, *r;
    int data;
}tree;

int n;
int a[1010];
int b[1010];
tree * c;

tree *creat(int *a, int *b, int t)
{
    tree *p;
    for(int i = 0; i < t; i ++)
    {
        if(a[0] == b[i])
        {
            p = (tree* )malloc(sizeof(tree));
            p ->data = a[0];
            p -> l = creat(a + 1, b, i);
            p -> r = creat(a + i + 1, b + i + 1, t - i - 1);
            return p;
        }
    }
    return NULL;
}

void out(tree *h)
{
    if(h != NULL)
    {
        out(h ->l);
        out(h ->r);
        if(h == c)
            printf("%d\n",h ->data);
        else
            printf("%d ",h ->data);
    }
}

int main()
{
    while(~scanf("%d", &n))
    {
        for(int i = 0; i < n; i ++)
        {
            scanf("%d",&a[i]);
        }
        for(int i = 0; i < n; i ++)
        {
            scanf("%d",&b[i]);
        }
        c = creat(a, b, n);
        out(c);
    }
    return 0;
}

    G - 美素数

小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识。
  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数。
  给定一个区间,你能计算出这个区间内有多少个美素数吗?

 

Input

第一行输入一个正整数T,表示总共有T组数据(T <= 10000)。
接下来共T行,每行输入两个整数L,R(1<= L <= R <= 1000000),表示区间的左值和右值。
 

Output

对于每组数据,先输出Case数,然后输出区间内美素数的个数(包括端点值L,R)。
每组数据占一行,具体输出格式参见样例。
 

Sample Input

    
    
3 1 100 2 2 3 19
 

Sample Output

  
  
Case #1: 14 Case #2: 1 Case #3: 4

    第一眼看到这个范围,我就已经不敢写了,就怕超时,后来鼓起勇气,用遍历 + 素数筛选 过了,343 ms。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define N 1000010

int prime[N];
bool is_prime[N + 10];
int beaprime[80000];  // 一开始计算过范围的。

void sieve(int n)  // 素数筛选
{
    int p = 0;
    for(int i = 0; i <= n; i ++)
        is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;
    for(int i = 2; i <= n; i ++)
    {
        if(is_prime[i])
            prime[p ++] = i;
        for(int j = 2* i; j <= n; j += i)
        {
            is_prime[j] = false;
        }
    }
}

int main()
{
    int t;
    int n, m;
    sieve(1000010);
    int coutt = 1;
    /*for(int i = 0; i < 10; i ++)
    {
        printf("prime  %d\n", prime[i]);
    }*/
    for(int i = 0; i <= 80000; i ++)
    {
        int ttp = prime[i];
        int hh = 0;
        while(ttp)
        {
            hh += ttp % 10;
            ttp = ttp / 10;
        }
        if(is_prime[hh])
            beaprime[coutt ++] = prime[i];  // 美素数、
    }

    while(~scanf("%d",&t))
    {
        int icase = 1;
        while(t --)
        {
            /*for(int i = 0; i < 10; i ++)
            {
                printf("beaprime  %d\n", beaprime[i]);
            }*/
            int cnt = 0;
            scanf("%d%d",&n, &m);
            for(int i = 1; i <= 80000; i ++)
            {
                if(beaprime[i] > m)
                break;
                if(beaprime[i] >= n && beaprime[i] <= m)
                {
                    cnt ++;
                }
            }
            printf("Case #%d: %d\n",icase ++, cnt);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值