Codeforces Round #131 (Div. 2) A B

A. System of Equations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?

You are given a system of equations:

You should count, how many there are pairs of integers (a, b) (0 ≤ a, b) which satisfy the system.

Input

A single line contains two integers n, m (1 ≤ n, m ≤ 1000) — the parameters of the system. The numbers on the line are separated by a space.

Output

On a single line print the answer to the problem.

Sample test(s)
Input
9 3
Output
1
Input
14 28
Output
1
Input
4 20
Output
0
Note

In the first sample the suitable pair is integers (3, 0). In the second sample the suitable pair is integers (3, 5). In the third sample there is no suitable pair.


水题。

#include<iostream>
#include<stdio.h>

using namespace std;

int a[100],m,n;
void init()
{
    int i;
    for(i=0;i<100;i++)
       a[i]=i*i;
}

int main()
{
    int sum,i;
    init();
    while(~scanf("%d%d",&n,&m))
    {
        sum=0;
        for(i=0;a[i]<=n;i++)
        {
            if(i+(n-a[i])*(n-a[i])==m)
            {
                sum++;
            }
         //   printf("%d %d\n",i,(n-a[i]));
        }
        cout<<sum<<endl;
    }
    return 0;
}

B. Hometask
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?

You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.

Each digit is allowed to occur in the number the same number of times it occurs in the set.

Input

A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.

Output

On a single line print the answer to the problem. If such number does not exist, then you should print -1.

Sample test(s)
Input
1
0
Output
0
Input
11
3 4 5 4 5 3 5 3 4 4 0
Output
5554443330
Input
8
3 2 5 1 5 2 2 3
Output
-1
Note

In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.

做的时候没写出来,这是某卖萌学长后来写出来的dp解法

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

int a[100005];
int dp[100005][3];

int main()
{
    int i,j,n,t;
    scanf("%d",&n);
    t=0;
    for (i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if (a[i]==0) t++;
    }
    if (t==0)
    {
        printf("-1\n");
        return 0;
    }
    if (t==n)
    {
        printf("0\n");
        return 0;
    }
    sort(a,a+n);
    memset(dp,-1,sizeof(dp));
    dp[t-1][0]=0;
    for (i=t;i<n;i++)
    {
        for (j=0;j<3;j++)
        {
            if (dp[i-1][j]==-1) continue;
            dp[i][(j+a[i])%3]=max(dp[i-1][j]+1,dp[i][(j+a[i])%3]);
            dp[i][j]=max(dp[i][j],dp[i-1][j]);
        }
    }

    if (dp[n-1][0]==-1 || dp[n-1][0]==0)
    {
        printf("0\n");
        return 0;
    }
    int tag=0;
    for (i=n-1;i>=t;i--)
    {
        if (dp[i][tag]==dp[i-1][(tag+3-a[i]%3)%3]+1 && dp[i][tag]!=0)
        {
            printf("%d",a[i]);
            tag=(tag+3-a[i]%3)%3;
        }
    }
    for (i=0;i<t;i++)
    {
        printf("0");
    }
    printf("\n");
    return 0;
}

C题没看懂题目意思,后面的题目就直接不想看。orz!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值