AtCoder Beginner Contest 042(Virtual Participation)

本菜鸟的情况: 


A - Iroha and Haiku (ABC Edition) 

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 100 points

Problem Statement

Iroha loves Haiku. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

To create a Haiku, Iroha has come up with three different phrases. These phrases have A, B and C syllables, respectively. Determine whether she can construct a Haiku by using each of the phrases once, in some order.

Constraints

  • 1≦A,B,C≦10

Input

The input is given from Standard Input in the following format:

A B C

Output

If it is possible to construct a Haiku by using each of the phrases once, print YES (case-sensitive). Otherwise, print NO.


Sample Input 1

5 5 7

Sample Output 1

YES

Using three phrases of length 5, 5 and 7, it is possible to construct a Haiku.


Sample Input 2

7 7 5

Sample Output 2

NO

 解题思路:

        这个题还是非常简单的,实际上就是要去判断输入的三个数是不是5,5,7(顺序无所谓),是的话则输出YES,否则的话输出NO。简单来说直接拿个数组,排序之后判断就可以了。

AC代码:

#include <bits/stdc++.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <algorithm>
#include <deque>
#include <set>
#include <iomanip>
#define ll long long
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define exp 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int N=3e6;

ll gcd(ll a,ll b)
{
	return b==0?a:gcd(b,a%b);
}
ll qpow(ll a,ll b)
{
	ll f=1;
	while (b)
	{
		if (b&1)
			f=f*a;
		a=a*a;
		b>>=1;
	}
	return f;
}

void solve()
{
	int a[3];
	for (int i=0;i<3;i++)
		cin>>a[i];
	sort(a,a+3);
	if (a[0]!=5||a[1]!=5||a[2]!=7)
		cout<<"NO";
	else
		cout<<"YES";
}

int main()
{
	solve();
	return 0;
}

B - Iroha Loves Strings (ABC Edition)

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 200 points

Problem Statement

Iroha has a sequence of N strings S1​,S2​,...,SN​. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s1​s2​s3​...sn​ is lexicographically smaller than another string t=t1​t2​t3​...tm​ if and only if one of the following holds:

  • There exists an index i(1≦i≦min(n,m)), such that sj​=tj​ for all indices j(1≦j<i), and si​<ti​.
  • si​=ti​ for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

  • 1 ≦ N, L ≦ 100
  • For each i, the length of Si​ equals L.
  • For each i, Si​ consists of lowercase letters.

Input

The input is given from Standard Input in the following format:

N L
S1​
S2​
:
SN​

Output

Print the lexicographically smallest string that Iroha can produce.


Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axxcxxdxx.


 

解题思路:

        别问我为啥错了这么多次,把拼接字符串的个数写成了L,害的我找了半天才找出来。这个题其实只需要用一个string数组,进行排序后一个一个拼接在后面就可以了。

AC代码:

#include <bits/stdc++.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <algorithm>
#include <deque>
#include <set>
#include <iomanip>
#define ll long long
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define exp 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int N=3e6;

ll gcd(ll a,ll b)
{
	return b==0?a:gcd(b,a%b);
}
ll qpow(ll a,ll b)
{
	ll f=1;
	while (b)
	{
		if (b&1)
			f=f*a;
		a=a*a;
		b>>=1;
	}
	return f;
}

void solve()
{
	int n,l;
	cin>>n>>l;
	string s[105];
	for (int i=0;i<n;i++)
		cin>>s[i];
	sort(s,s+n);
	string t="";
	for (int i=0;i<n;i++)
		t+=s[i];
	cout<<t;
}

int main()
{
	solve();
	return 0;
}

C - Iroha's Obsession

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 300 points

Problem Statement

Iroha is very particular about numbers. There are K digits that she dislikes: D1​,D2​,...,DK​.

She is shopping, and now paying at the cashier. Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change).

However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.

Find the amount of money that she will hand to the cashier.

Constraints

  • 1 ≦ N < 10000
  • 1 ≦ K < 10
  • 0≦D1​<D2​<…<DK​≦9
  • {D1,D2,...,DK} ≠ {1,2,3,4,5,6,7,8,9}

Input

The input is given from Standard Input in the following format:

N K
D1​ D2​ … DK​

Output

Print the amount of money that Iroha will hand to the cashier.


Sample Input 1

1000 8
1 3 4 5 6 7 8 9

Sample Output 

2000

She dislikes all digits except 0 and 2.

The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2, is 2000.


Sample Input 2

9999 1
0

Sample Output 2

9999

解题思路:

        这个题我一开始想得太复杂了,后来才反应过来直接暴力解就可以。先把D数组中出现的数标记一下,之后一位一位的进行比较,看看每一位的数字是否都不在D中出现。直到出现正确答案为止。

AC代码:

#include <bits/stdc++.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <algorithm>
#include <deque>
#include <set>
#include <iomanip>
#define ll long long
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define exp 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int N=3e6;

ll gcd(ll a,ll b)
{
	return b==0?a:gcd(b,a%b);
}
ll qpow(ll a,ll b)
{
	ll f=1;
	while (b)
	{
		if (b&1)
			f=f*a;
		a=a*a;
		b>>=1;
	}
	return f;
}

void solve()
{
	int n,k;
	cin>>n>>k;
	int a[10],b[10];
	memset(b,0,sizeof(b));
	for (int i=0;i<k;i++)
	{
		cin>>a[i];
		b[a[i]]++;
	}
	while (true)
	{
		int x=n;
		int f=0;
		while (x)
		{
			int y=x%10;
			x/=10;
			if (b[y])
			{
				f=1;
				break;
			}
		}
		if (!f)
		{
			cout<<n;
			break;
		}
		n++;
	}
}

int main()
{
	solve();
	return 0;
}

D - Iroha and a Grid

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 400 points

Problem Statement

We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.

However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells.

Find the number of ways she can travel to the bottom-right cell.

Since this number can be extremely large, print the number modulo 10^{9}+7.

Constraints

  • 1 ≦ H, W ≦ 100,000
  • 1≦A<H
  • 1 ≦ B < W

Input

The input is given from Standard Input in the following format:

H W A B

Output

Print the number of ways she can travel to the bottom-right cell, modulo 10^{9}+7.


Sample Input 1

2 3 1 1

Sample Output 1

2

We have a 2×3 grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".


Sample Input 2

10 7 3 4

Sample Output 2

3570

There are 12 forbidden cells.


Sample Input 3

100000 100000 99999 99999

Sample Output 3

1

Sample Input 4

100000 100000 44444 55555

Sample Output 4

738162020

解题思路:

        这个题在一开始没有做出来。开始的思想很简单,想着用dp直接做,但是都失败了,因为这个二维数组开得太大,结束之后看题解才发现了一个类似于杨辉三角的模型,正解应该是拿类似于排列组合的思想。这个就涉及到组合数学中的常用算法,此处应用的是组合问题(有关组合数学的相关知识点),即C_{n}^{m}的计算,套路是很固定的,直接套模板即可。

        方案数的求解

AC代码:

#include <bits/stdc++.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <algorithm>
#include <deque>
#include <set>
#include <iomanip>
#define ll long long
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define exp 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int mod=1e9+7;
const int N=1e5+5;
ll fnv[2*N],fac[2*N]={1};

ll gcd(ll a,ll b)
{
	return b==0?a:gcd(b,a%b);
}
ll qpowmod(ll a,ll b)
{
	ll f=1%mod;
	while (b)
	{
		if (b&1)
			f=f*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return f;
}

void pre()
{
	for (int i=1;i<=2*N;i++)
		fac[i]=fac[i-1]*i%mod;
	fnv[0]=1;
	fnv[2*N]=qpowmod(fac[2*N],mod-2);
	for (int i=2*N-1;i>=1;i--)
		fnv[i]=fnv[i+1]*(i+1)%mod;
}

ll C(int m,int n)
{
	return fac[m]*fnv[n]%mod*fnv[m-n]%mod;
}
//注释掉的部分是一个错误的代码,也是在做题的时候第一时间想到的思路代码
//ll dp[99999][99999];
//void solve()
//{
//	int h,w,a,b;
//	cin>>h>>w>>a>>b;
//	for (int i=1;i<=h;i++)
//		dp[i][1]=1;
//	for (int i=1;i<=w;i++)
//		dp[1][i]=1;
//	for (int i=h;i>=h-a+1;i--)
//	{
//		for (int j=1;j<=b;j++)
//			dp[i][j]=0;
//	}
//	for (int i=2;i<=h;i++)
//	{
//		for (int j=2;j<=w;j++)
//		{
//			if (i<=h-a||j>b)
//				dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
//		}
//	}
//	cout<<dp[h][w];
//}
void solve()
{
	int h,w,a,b;
	cin>>h>>w>>a>>b;
	pre();
	ll ans=0;
	for (int i=b+1;i<=w;i++)
		ans=(ans+C(h-a+i-2,i-1)*C(a+w-i-1,a-1)%mod)%mod;
	cout<<ans;
}

int main()
{
	solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值