AtCoder Beginner Contest 280

AtCoder Beginner Contest 280

A - Pawn on a Grid

Problem Statement

There is a grid with H H H rows from top to bottom and W W W columns from left to right. Each square has a piece placed on it or is empty.

The state of the grid is represented by H H H strings S 1 , S 2 , … , S H S_1, S_2, \ldots, S_H S1,S2,,SH, each of length W W W.
If the j j j-th character of S i S_i Si is #, the square at the i i i-th row from the top and j j j-th column from the left has a piece on it;
if the j j j-th character of S i S_i Si is ., the square at the i i i-th row from the top and j j j-th column from the left is empty.

How many squares in the grid have pieces on them?

Constraints

  • 1 ≤ H , W ≤ 10 1\leq H,W \leq 10 1H,W10
  • H H H and W W W are integers.
  • S i S_i Si is a string of length W W W consisting of # and ..

Input

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

H   W   S 1   S 2   ⋮   S H H~W~S_1~S_2~⋮~S_H H W S1 S2  SH

Output

Print the number of squares with pieces as an integer.


Sample Input 1
3 5
#....
.....
.##..
Sample Output 1
3

The following three squares have pieces on them:

  • the square at the 1 1 1-st row from the top and 1 1 1-st column from the left;
  • the square at the 3 3 3-rd row from the top and 2 2 2-nd column from the left;
  • the square at the 3 3 3-rd row from the top and 3 3 3-rd column from the left.

Thus, 3 3 3 should be printed.


Sample Input 2
1 10
..........
Sample Output 2
0

Since no square has a piece on it, 0 0 0 should be printed.


Sample Input 3
6 5
#.#.#
....#
..##.
####.
..#..
#####
Sample Output 3
16

分析:

统计’#'个数。

#include<bits/stdc++.h>
using namespace std;
int h,w;
string s;
int main(){
	cin>>h>>w;
	int num=0;
	for(int i=1;i<=h;++i){
		cin>>s;
		for(int j=0;j<w;++j)
			if(s[j]=='#')
				++num;
	}cout<<num<<endl;
	return 0;
}

B - Inverse Prefix Sum

Problem Statement

You are given an integer N N N and a sequence S = ( S 1 , … , S N ) S=(S_1,\ldots,S_N) S=(S1,,SN) of length N N N.

Find a sequence A = ( A 1 , … , A N ) A=(A_1,\ldots,A_N) A=(A1,,AN) of length N N N that satisfies the following condition for all k = 1 , … , N k=1,\ldots,N k=1,,N:

  • A 1 + A 2 + … + A k = S k A_1+A_2+\ldots+A_k = S_k A1+A2++Ak=Sk.

Such a sequence A A A always exists and is unique.

Constraints

  • 1 ≤ N ≤ 10 1 \leq N \leq 10 1N10
  • − 1 0 9 ≤ S i ≤ 1 0 9 -10^9\leq S_i \leq 10^9 109Si109
  • All values in the input are integers.

Input

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

N   S 1   …   S N N~S_1~\ldots~S_N N S1  SN

Output

Print the elements of a sequence A = ( A 1 , … , A N ) A=(A_1,\ldots,A_N) A=(A1,,AN) that satisfies all the conditions in order, separated by spaces.


Sample Input 1
3
3 4 8
Sample Output 1
3 1 4

The sequence in the output actually satisfies all the conditions:

  • A 1 = 3 = S 1 A_1=3=S_1 A1=3=S1;
  • A 1 + A 2 = 3 + 1 = 4 = S 2 A_1+A_2=3+1=4=S_2 A1+A2=3+1=4=S2;
  • A 1 + A 2 + A 3 = 3 + 1 + 4 = 8 = S 3 A_1+A_2+A_3=3+1+4=8=S_3 A1+A2+A3=3+1+4=8=S3.

Sample Input 2
10
314159265 358979323 846264338 -327950288 419716939 -937510582 97494459 230781640 628620899 -862803482
Sample Output 2
314159265 44820058 487285015 -1174214626 747667227 -1357227521 1035005041 133287181 397839259 -1491424381

分析:

给前缀和数组,求原数组。

#include<bits/stdc++.h>
using namespace std;
int n,s[11];
int main(){
	cin>>n;
	cin>>s[1];
	cout<<s[1];
	for(int i=2;i<=n;++i){
		cin>>s[i];
		cout<<" "<<s[i]-s[i-1];
	}cout<<endl;
	return 0;
}

C - Extra Character

Problem Statement

You are given strings S S S and T T T. S S S consists of lowercase English letters, and T T T is obtained by inserting a lowercase English letter into S S S.

Find the position of the inserted character in T T T
If there are multiple candidates, find any of them.

Constraints

  • 1 ≤ ∣ S ∣ ≤ 5 × 1 0 5 1 \leq |S| \leq 5\times 10^5 1S5×105
  • S S S consists of lowercase English letters.
  • T T T is obtained by inserting a lowercase English letter into S S S.

Input

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

S   T S~T S T

Output

Print an integer i i i, representing that the inserted character is the i i i-th character from the beginning of T T T. If there are multiple possible answers, printing any of them is accepted.


Sample Input 1
atcoder
atcorder
Sample Output 1
5

The 5 5 5-th character from the beginning of T T T, r, is inserted.


Sample Input 2
million
milllion
Sample Output 2
5

One of the 3 3 3-rd, 4 4 4-th, and 5 5 5-th characters from the beginning of T T T is inserted. Thus, printing any one of 3 3 3, 4 4 4, and 5 5 5 is accepted.


Sample Input 3
vvwvw
vvvwvw
Sample Output 3
3

分析:

题意:字符串 S S S插入一个字符变成字符串 T T T,问插入的位置;人话:从前向后找第一个不一样的。(下标从 1 1 1开始)

#include<bits/stdc++.h>
using namespace std;
string s,t;
int main(){
	cin>>s>>t;
	s+='C';//防越界
	for(int i=0;i<s.length();++i)
		if(s[i]!=t[i]){
			cout<<i+1<<endl;
			break;
		}
	return 0;
}

前三题代码量居然是递减的,人都看傻了。

D - Factorial and Multiple

Problem Statement

You are given an integer K K K greater than or equal to 2 2 2.
Find the minimum positive integer N N N such that N ! N! N! is a multiple of K K K.

Here, N ! N! N! denotes the factorial of N N N. Under the Constraints of this problem, we can prove that such an N N N always exists.

Constraints

  • 2 ≤ K ≤ 1 0 12 2\leq K\leq 10^{12} 2K1012
  • K K K is an integer.

Input

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

K K K

Output

Print the minimum positive integer N N N such that N ! N! N! is a multiple of K K K.


Sample Input 1
30
Sample Output 1
5
  • 1 ! = 1 1!=1 1!=1
  • 2 ! = 2 × 1 = 2 2!=2\times 1=2 2!=2×1=2
  • 3 ! = 3 × 2 × 1 = 6 3!=3\times 2\times 1=6 3!=3×2×1=6
  • 4 ! = 4 × 3 × 2 × 1 = 24 4!=4\times 3\times 2\times 1=24 4!=4×3×2×1=24
  • 5 ! = 5 × 4 × 3 × 2 × 1 = 120 5!=5\times 4\times 3\times 2\times 1=120 5!=5×4×3×2×1=120

Therefore, 5 5 5 is the minimum positive integer N N N such that N ! N! N! is a multiple of 30 30 30. Thus, 5 5 5 should be printed.


Sample Input 2
123456789011
Sample Output 2
123456789011

Sample Input 3
280
Sample Output 3
7

分析:

给一个 K K K,求最小的正整数阶乘 N ! N! N!中的 N N N是多少。高精度大概能过39/63个点。

可以先给 K K K质因数分解,假设分解成 2 20 ⋅ 7 1 2^{20}·7^1 22071,那么,想要有 20 20 20 2 2 2,可以从 2 2 2向后找其倍数,每个倍数都算一遍分解后 2 2 2的指数是多少,超过就停下,给每个质数算出来的值打个擂台就行了。

原本的代码:

当时以为 2 20 2^{20} 220一定要达到 40 40 40,四个人一小时都没看出来哪错了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll k,ans=0;
vector<ll>p;
vector<int>n;
int main(){
	cin>>k;
	for(ll i=2;i*i<=k;++i){
		if(k%i!=0)
			continue;
		p.push_back(i);
		int num=0;
		while(k%i==0){
			++num;
			k/=i;
		}n.push_back(num);
	}p.push_back(k),n.push_back(1);
	for(int i=0;i<p.size();++i)
		ans=max(ans,p[i]*n[i]);
	cout<<ans<<endl;
	return 0;
}
正确的代码:

(标题取名天才)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll k,ans=0;
vector<ll>p;
vector<int>n;
int main(){
	cin>>k;
	for(ll i=2;i*i<=k;++i){//质因数分解
		if(k%i!=0)
			continue;
		p.push_back(i);
		int num=0;
		while(k%i==0){
			++num;
			k/=i;
		}n.push_back(num);
	}if(k!=1)//如果k没有被分完,那么剩下的一定是个指数为1的大质数
		p.push_back(k),n.push_back(1);
	for(int i=0;i<p.size();++i){
		ll x=0,num=0;
		while(num<n[i]){
			x+=p[i];//依次找倍数
			ll m=x;
			while(m%p[i]==0)
				++num,m/=p[i];//硬核计算有多少个p[i]连乘
		}ans=max(ans,x);//打擂台
	}cout<<ans<<endl;
	return 0;
}

E - Critical Hit

Problem Statement

There is a monster with initial stamina N N N.
Takahashi repeatedly attacks the monster while the monster’s stamina remains 1 1 1 or greater.

An attack by Takahashi reduces the monster’s stamina by 2 2 2 with probability P 100 \frac{P}{100} 100P and by 1 1 1 with probability 1 − P 100 1-\frac{P}{100} 1100P.

Find the expected value, modulo 998244353 998244353 998244353 (see Notes), of the number of attacks before the monster’s stamina becomes 0 0 0 or less.

Notes

We can prove that the sought expected value is always a finite rational number. Moreover, under the Constraints of this problem, when the value is represented as P Q \frac{P}{Q} QP by two coprime integers P P P and Q Q Q, we can show that there exists a unique integer R R R such that R × Q ≡ P ( m o d 998244353 ) R \times Q \equiv P\pmod{998244353} R×QP(mod998244353) and 0 ≤ R < 998244353 0 \leq R \lt 998244353 0R<998244353. Print such R R R.

Constraints

  • 1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2\times 10^5 1N2×105
  • 0 ≤ P ≤ 100 0 \leq P \leq 100 0P100
  • All values in the input are integers.

Input

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

N   P N~P N P

Output

Find the expected value, modulo 998244353 998244353 998244353, of the number of Takahashi’s attacks.


Sample Input 1
3 10
Sample Output 1
229596204

An attack by Takahashi reduces the monster’s stamina by 2 2 2 with probability 10 100 = 1 10 \frac{10}{100}=\frac{1}{10} 10010=101 and by 1 1 1 with probability 100 − 10 100 = 9 10 \frac{100-10}{100}=\frac{9}{10} 10010010=109.

  • The monster’s initial stamina is 3 3 3.
  • After the first attack, the monster’s stamina is 2 2 2 with probability 9 10 \frac{9}{10} 109 and 1 1 1 with probability 1 10 \frac{1}{10} 101.
  • After the second attack, the monster’s stamina is 1 1 1 with probability 81 100 \frac{81}{100} 10081 with probability 18 100 \frac{18}{100} 10018, and -1−1 with probability 1 100 \frac{1}{100} 1001. With probability 18 100 + 1 100 = 19 100 \frac{18}{100}+\frac{1}{100}=\frac{19}{100} 10018+1001=10019, the stamina becomes 0 0 0 or less, and Takahashi stops attacking after two attacks.
  • If the stamina remains 1 1 1 after two attacks, the monster’s stamina always becomes 0 0 0 or less by the third attack, so he stops attacking after three attacks.

Therefore, the expected value is 2 × 19 100 + 3 × ( 1 − 19 100 ) = 281 100 2\times \frac{19}{100}+3\times\left(1-\frac{19}{100}\right)=\frac{281}{100} 2×10019+3×(110019)=100281. Since 229596204 × 100 ≡ 281 ( m o d 998244353 ) 229596204 \times 100 \equiv 281\pmod{998244353} 229596204×100281(mod998244353), print 229596204 229596204 229596204.


Sample Input 2
5 100
Sample Output 2
3

Takahashi’s attack always reduces the monster’s stamina by 2 2 2. After the second attack, the stamina remains 5 − 2 × 2 = 1 5-2\times 2=1 52×2=1, so the third one is required.


Sample Input 3
280 59
Sample Output 3
567484387

分析:

E [ i ] = ( E [ i − 1 ] + 1 ) × P 100 + ( E [ i − 2 ] + 1 ) × ( 1 − P 100 ) , i > 1 E[i]=(E[i-1]+1)\times\frac{P}{100}+(E[i-2]+1)\times(1-\frac{P}{100}),i>1 E[i]=(E[i1]+1)×100P+(E[i2]+1)×(1100P),i>1

E [ 0 ] = 0 , E [ 1 ] = P 100 E[0]=0,E[1]=\frac{P}{100} E[0]=0,E[1]=100P

用结构体重载写分数,用扩展欧几里得求同余方程。

R × Q ≡ P ( m o d K ) , K = 998244353 R\times Q\equiv P\pmod{K},K=998244353 R×QP(modK),K=998244353

R 1 × Q = P + R 2 × K R_1\times Q=P+R_2\times K R1×Q=P+R2×K

R 1 × Q − R 2 × K = P R_1\times Q-R_2\times K=P R1×QR2×K=P

R 1 P × Q − R 2 P × K = 1 \frac{R_1}{P}\times Q-\frac{R_2}{P}\times K=1 PR1×QPR2×K=1

但是这 E E E数组分子分母能达到 1 0 1 0 5 10^{10^5} 10105。高精没试,估计会超时。逆元不会。

A C : W A : R E = 12 : 20 : 7 AC:WA:RE=12:20:7 AC:WA:RE=12:20:7 (悲)

#include<bits/stdc++.h>
using namespace std;
#define MAXN 200010
#define ll long long
int n;
const ll m=998244353;//模
struct nod{
	ll f1,f2;//分子,分母
	nod(ll _f1=0,ll _f2=1):f1(_f1),f2(_f2){}
	ll gcd(ll x,ll y){
		return x%y==0?y:gcd(y,x%y);//用于约分的最大公因数
	}void red(){//约分
		ll g=gcd(f1,f2);
		f1/=g,f2/=g;
		return;
	}nod operator +(const int x){
		return nod(f1+x*f2,f2);
	}nod operator +(const nod x){
		ll g=gcd(f2,x.f2);
		ll f=f2*x.f2/g;
		nod ans=nod(f/f2*f1+f/x.f2*x.f1,f);
		ans.red();
		return ans;
	}nod operator *(const nod x){
		nod ans=nod(f1*x.f1,f2*x.f2);
		ans.red();
		return ans;
	}//重载
}p1,p2,E[MAXN];
ll ex_gcd(ll a,ll b,ll &x,ll &y){//扩欧
	if(b==0){
		x=1;
		y=0;
		return a;
	}ll r=ex_gcd(b,a%b,x,y);
	ll t=x;
	x=y;
	y=t-a/b*y;
	return r;
}
int main(){
	cin>>n>>p2.f1;
	p2.f2=100;
	p2.red();//伤害为2的概率
	p1=nod(p2.f2-p2.f1,p2.f2);//伤害为1的概率
	E[1]=p1;
	for(int i=2;i<=n-1;++i)
		E[i]=(E[i-1]+1)*p1+(E[i-2]+1)*p2;//递推
	E[n]=E[n-1]+1;//还剩1血时,1只因毙命,次数直接+1,不考虑伤害
	ll ans1,ans2;
	ex_gcd(E[n].f2,m,ans1,ans2);
	cout<<(ans1+m)*E[n].f1%m<<endl;//
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值