賽前練習(百度之星資格賽及初賽真題+Codeforces(div2級別))

今夕何夕(日期問題)

 

今天是2017年8月6日,農曆閏六月十五。

小度獨自憑欄,望着一輪圓月,發出了“今夕何夕,見此良人”的寂寞感慨。

爲了排遣鬱結,它決定思考一個數學問題:接下來最近的哪一年裏的同一個日子,和今天的星期數一樣?比如今天是8月6日,星期日。下一個也是星期日的8月6日發生在2023年。

小貼士:在公曆中,能被4整除但不能被100整除,或能被400整除的年份即爲閏年。

Input

第一行爲T,表示輸入數據組數。

每組數據包含一個日期,格式爲YYYY-MM-DD。

1 ≤ T ≤ 10000

YYYY ≥ 2017

日期一定是個合法的日期
 

Output

對每組數據輸出答案年份,題目保證答案不會超過四位數。

Sample Input

3
2017-08-06
2017-08-07
2018-01-01

Sample Output

2023
2023
2024
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y,m,d,sum,same;
bool check(int y){
	if(y%400==0||y%100!=0&&y%4==0) return true;
	else return false;
}
bool judge(int yy,int mm,int dd){
	if(same==1) return false;//第一次判斷的是自己same=1直接false返回
	else if(mm==m&&dd==d&&sum%7==0) return true;
	else return false;
}
void deal(){
	sum=0,same=1;
	int yy=y,mm=m,dd=d;
	while(!judge(yy,mm,dd)){
		same=0;
		dd++;
		sum++;
		if(check(yy)) a[2]=29;
		else a[2]=28;
		if(dd>a[mm]){
			dd=1;
			mm++;
			if(mm>12){
				mm=1;
				yy++;
			}
		}
	}
	printf("%04d\n",yy,mm,dd);                         
}
int main(){
	int n;
	cin>>n;
	while(n--){
		scanf("%d-%d-%d",&y,&m,&d);
		deal();
	}    
	return 0;
} 

 

 

 

 

度度熊的01世界(連通塊+思維)

 

度度熊是一個喜歡計算機的孩子,在計算機的世界中,所有事物實際上都只由0和1組成。

現在給你一個n*m的圖像,你需要分辨他究竟是0,還是1,或者兩者均不是。

圖像0的定義:存在1字符且1字符只能是由一個連通塊組成,存在且僅存在一個由0字符組成的連通塊完全被1所包圍。

圖像1的定義:存在1字符且1字符只能是由一個連通塊組成,不存在任何0字符組成的連通塊被1所完全包圍。

連通的含義是,只要連續兩個方塊有公共邊,就看做是連通。

完全包圍的意思是,該連通塊不與邊界相接觸。

Input

本題包含若干組測試數據。
每組測試數據包含:
第一行兩個整數n,m表示圖像的長與寬。
接下來n行m列將會是隻有01組成的字符畫。

滿足1<=n,m<=100

Output

如果這個圖是1的話,輸出1;如果是0的話,輸出0,都不是輸出-1。

Sample Input

32 32
00000000000000000000000000000000
00000000000111111110000000000000
00000000001111111111100000000000
00000000001111111111110000000000
00000000011111111111111000000000
00000000011111100011111000000000
00000000111110000001111000000000
00000000111110000001111100000000
00000000111110000000111110000000
00000001111110000000111110000000
00000001111110000000011111000000
00000001111110000000001111000000
00000001111110000000001111100000
00000001111100000000001111000000
00000001111000000000001111000000
00000001111000000000001111000000
00000001111000000000000111000000
00000000111100000000000111000000
00000000111100000000000111000000
00000000111100000000000111000000
00000001111000000000011110000000
00000001111000000000011110000000
00000000111000000000011110000000
00000000111110000011111110000000
00000000111110001111111100000000
00000000111111111111111000000000
00000000011111111111111000000000
00000000111111111111100000000000
00000000011111111111000000000000
00000000001111111000000000000000
00000000001111100000000000000000
00000000000000000000000000000000
32 32
00000000000000000000000000000000
00000000000000001111110000000000
00000000000000001111111000000000
00000000000000011111111000000000
00000000000000111111111000000000
00000000000000011111111000000000
00000000000000011111111000000000
00000000000000111111110000000000
00000000000000111111100000000000
00000000000001111111100000000000
00000000000001111111110000000000
00000000000001111111110000000000
00000000000001111111100000000000
00000000000011111110000000000000
00000000011111111110000000000000
00000001111111111111000000000000
00000011111111111111000000000000
00000011111111111111000000000000
00000011111111111110000000000000
00000000001111111111000000000000
00000000000000111111000000000000
00000000000001111111000000000000
00000000000111111110000000000000
00000000000011111111000000000000
00000000000011111111000000000000
00000000000011111111100000000000
00000000000011111111100000000000
00000000000000111111110000000000
00000000000000001111111111000000
00000000000000001111111111000000
00000000000000000111111111000000
00000000000000000000000000000000
3 3
101
101
011

Sample Output

0
1
-1

題目分析:我們用cnt1代表1的連通塊的個數,cnt0代表內層的0的連通塊的個數,那麼要識別出0,就要內層的cnt0=1且1的連通塊的個數爲1,要識別出1,那麼就應該沒有內層的連通塊,且1的連通塊個數爲1。其餘情況輸出-1

注意只抓關鍵部分——只看1和內部0的連通塊的個數,而不關心1外的0的連通塊數

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int n,m,cnt1,cnt0,in;
char mp[105][105];
int vis[105][105];
int mov[4][2]= {0,1,0,-1,1,0,-1,0};
void dfs(int x,int y,char ch) {
	for(int i=0; i<4; i++) {
		int nx=x+mov[i][0];
		int ny=y+mov[i][1];
		if(nx>=n||nx<0||ny>=m||ny<0) {//如果它的下一步超出邊界,那麼它就是邊界
			if(ch=='0') {
				in=0;
				continue;
			}
		}
		if(mp[nx][ny]==ch&&vis[nx][ny]==0) {//若字符相同且未被標記
			vis[nx][ny]=1;
			dfs(nx,ny,ch);
		}
	}
}
int main() {
	while(~scanf("%d%d",&n,&m)) {
		cnt1=cnt0=0;
		memset(mp,0,sizeof mp);
		memset(vis,0,sizeof vis);
		for(int i=0; i<n; i++)
			scanf("%s",&mp[i]);
		for(int i=0; i<n; i++) {
			for(int j=0; j<m; j++) {
				if(!vis[i][j]) {
					if(mp[i][j]=='0') {
						in=1;
						vis[i][j]=1;
						dfs(i,j,mp[i][j]);
						if(in) cnt0++;
					} else if(mp[i][j]=='1') {
						vis[i][j]=1;
						dfs(i,j,mp[i][j]);
						cnt1++;
					}
				}
			}
		}
		if(cnt1!=1) cout<<"-1\n";//無論是1還是0,1的連通塊的個數必定是1
		else {
			if(cnt0==0) cout<<"1\n";//1內不含0的連通塊
			else if(cnt0==1) cout<<"0\n";//1內僅含一個0的連通塊
			else cout<<"-1\n";
		}
	}
	return 0;
}

 

 

 

 

 

度度熊的王國戰略(連通圖)

 

度度熊國王率領着喵哈哈族的勇士,準備進攻嘩啦啦族。

嘩啦啦族是一個強悍的民族,裏面有充滿智慧的謀士,擁有無窮力量的戰士。

所以這一場戰爭,將會十分艱難。

爲了更好的進攻嘩啦啦族,度度熊決定首先應該從內部瓦解嘩啦啦族。

第一步就是應該使得嘩啦啦族內部不能同心齊力,需要內部有間隙。

嘩啦啦族一共有n個將領,他們一共有m個強關係,摧毀每一個強關係都需要一定的代價。

現在度度熊命令你需要摧毀一些強關係,使得內部的將領,不能通過這些強關係,連成一個完整的連通塊,以保證戰爭的順利進行。

請問最少應該付出多少的代價。

Input

本題包含若干組測試數據。

第一行兩個整數n,m,表示有n個將領,m個關係。

接下來m行,每行三個整數u,v,w。表示u將領和v將領之間存在一個強關係,摧毀這個強關係需要代價w

數據範圍:

2<=n<=3000

1<=m<=100000

1<=u,v<=n

1<=w<=1000

Output

對於每組測試數據,輸出最小需要的代價。

Sample Input

2 1
1 2 1
3 3
1 2 5
1 2 4
2 3 3

Sample Output

1
3

題目分析:求隔斷某些點需要的最小代價——未指出具體幾個,只說最小,當然就是隔斷一個點的情況了

但這題有特殊情況——隔斷的兩條邊沒有公共點即不合題意,但改代碼也不失爲一種解題策略,僅供參考

#include<bits/stdc++.h>
using namespace std;
vector<string> vs;
int num[3005];
int main() {
	int n,m,u,v,val;
	while(~scanf("%d%d",&n,&m)) {
		memset(num,0,sizeof num);
		for(int i=0; i<m; i++) {
			scanf("%d%d%d",&u,&v,&val);
			if(u==v) continue;
			num[u]+=val;
			num[v]+=val;
		}
		sort(num+1,num+1+n);//將領編號從一開始 
		cout<<num[1]<<endl;
	}
	return 0;
}

 

 

 

 

 

P1m2(二分)

 

度度熊很喜歡數組!

我們稱一個整數數組爲穩定的,若且唯若其同時符合以下兩個條件:

1. 數組裏面的元素都是非負整數。
2. 數組裏面最大的元素跟最小的元素的差值不超過 1。

舉例而言,[1,2,1,2][1,2,1,2] 是穩定的,而 [−1,0,−1][−1,0,−1] 跟 [1,2,3][1,2,3] 都不是。

現在,定義一個在整數數組進行的操作:

* 選擇數組中兩個不同的元素 a以及 b ,將 a 減去 2 ,以及將 b 加上 1 。

舉例而言,[1,2,3][1,2,3] 經過一次操作後,有可能變爲 [−1,2,4][−1,2,4] 或 [2,2,1][2,2,1] 。

現在給定一個整數數組,在任意進行操作後,請問在所有可能達到的穩定數組中,擁有最大的『數組中的最小值』的那些數組,此值是多少呢?

Input

輸入的第一行有一個正整數 TT ,代表接下來有幾組測試數據。

對於每組測試數據:
第一行有一個正整數 NN 。
接下來的一行有 NN 個非負整數 xixi ,代表給定的數組。

* 1≤N≤3×1051≤N≤3×105
* 0≤xi≤1080≤xi≤108
* 1≤T≤181≤T≤18
* 至多 11 組測試數據中的 N>30000N>30000

Output

對於每一組測試數據,請依序各自在一行內輸出一個整數,代表可能到達的平衡狀態中最大的『數組中的最小值』,如果無法達成平衡狀態,則輸出 −1−1 。

Sample Input

2
3
1 2 4
2
0 100000000

Sample Output

2
33333333

 

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int MAX=3e5;
ll a[MAX+5];
int n;
bool judge(ll mid){//你要求的是一個最小值 
	ll cnt1=0,cnt2=0;
	for(int i=0;i<n;i++){
		if(a[i]<mid) cnt1+=mid-a[i];//所有比mid小的至少要達到mid 
		else if(a[i]>mid) cnt2+=(a[i]-mid)/2;//所有比mid大的要達到mid或mid+1 
	}//注意這個狀態一定會達到 
	if(cnt2>=cnt1) return true;
	return false;
}
int main() {
	int t;
	cin>>t;
	while(t--) {
		cin>>n;
		ll l=99999999,r=-1;
		for(int i=0; i<n; i++){
			scanf("%lld",&a[i]);
			l=min(l,a[i]);
			r=max(r,a[i]);
		}
		ll mid,sum=0;
		while(l<=r){
			mid=(l+r)>>1;
			if(judge(mid)) l=mid+1;
			else r=mid-1;
 		}
		cout<<(l+r)/2<<endl;
	}
	return 0;
}

 

 

 

 

 

King Moves(情況考慮)

 

The only king stands on the standard chess board. You are given his position in format "cd", where c is the column from 'a' to 'h' and d is the row from '1' to '8'. Find the number of moves permitted for the king.

Check the king's moves here https://en.wikipedia.org/wiki/King_(chess).

King moves from the position e4

Input

The only line contains the king's position in the format "cd", where 'c' is the column from 'a' to 'h' and 'd' is the row from '1' to '8'.

Output

Print the only integer x — the number of moves permitted for the king.

Example

Input

e4

Output

8

思路:三種情況——四個點、除此之外的四條邊上的點、剩下的點

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main(){
	string s;
	cin>>s;
	if(s[0]=='a'&&s[1]=='8'||s[0]=='h'&&s[1]=='8'||s[0]=='a'&&s[1]=='1'||s[0]=='h'&&s[1]=='1') cout<<"3\n";
	else if(s[1]=='8'||s[1]=='1'||s[0]=='a'||s[0]=='h') cout<<"5\n";
	else cout<<"8\n";
	return 0;
} 

 

 

 

 

 

Optimal Point on a Line(“舉一反三”的思維題目)

You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line.

The second line contains n integers xi ( - 109 ≤ xi ≤ 109) — the coordinates of the given n points.

Output

Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.

Example

Input

4
1 2 3 4

Output

2

思路:選一個點,使其他所有點到該點距離之和最小——選排序後位於中間位置的點

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm> 
#define ll long long
using namespace std;
const int MAX=3e5;
ll a[MAX+5];
int main() {
	int n;
	cin>>n;
	for(int i=0;i<n;i++) scanf("%lld",&a[i]);
	sort(a,a+n);
	if(n%2) cout<<a[n/2];
	else cout<<a[n/2-1];
	return 0;
}

 

 

 

 

 

Magic Odd Square(規律題)

Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.

Input

The only line contains odd integer n (1 ≤ n ≤ 49).

Output

Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.

Examples

Input

1

Output

1

Input

3

Output

2 1 4
3 5 7
6 9 8

題意:將1到n個不同數字放到一個nn的方格里,並保證每行、列、主對角線之和爲奇數。由於題目要求到n爲奇數,並且兩奇數相乘也爲奇數,則1到n*n裏面的奇數總數比偶數多1。

構造幻方——規律如圖

 

#include<bits/stdc++.h>
using namespace std;
#define eps 1e-6
int mp[55][55];
int main() {
	int n,col,num=1;
	cin>>n;
	for(int i=1;i<=n/2;i++){//填上半部分奇數
		col=n/2+i;
		for(int j=n/2-i+2;j<=col;j++){
			mp[i][j]=num;
			num+=2;
		}
	}
	int cnt=0;
	for(int i=n/2+1;i<=n;i++){ //填下半部分奇數
		col=1+cnt;
		for(int j=col;j<=n-col+1;j++){
			mp[i][j]=num;
			num+=2;
		}
		cnt++;
	}
	num=2;
	for(int i=1;i<=n;i++){//補充偶數並直接輸出
		for(int j=1;j<=n;j++){
			if(mp[i][j]==0){
				cout<<num<<' ';
				num+=2;
			} 
			else cout<<mp[i][j]<<' ';
		}
		cout<<endl;
	}
	return 0;
}

 

 

 

 

 

Fashion in Berland(讀懂題就OK)

According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open.

You are given a jacket with n buttons. Determine if it is fastened in a right way.

Input

The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket.

The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1.

Output

In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO".

Examples

Input

3
1 0 1

Output

YES

Input

3
1 0 0

Output

NO

題意:檢查安全帶是否系對(只有一個沒繫上或者當且僅當只有一個安全帶並繫上)

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main(){
	int n,x;
	cin>>n;
	int cnt0=0,cnt1=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		if(x==0) cnt0++;
		else if(x==1) cnt1++;
	}
	if(n==1&&cnt1==1||n>1&&cnt0==1) cout<<"YES\n";
	else cout<<"NO\n";
	return 0;
} 

 

 

 

 

 

s-palindrome(認真讀題&查看樣例)

Let's call a string "s-palindrome" if it is symmetric about the middle of the string. For example, the string "oHo" is "s-palindrome", but the string "aa" is not. The string "aa" is not "s-palindrome", because the second half of it is not a mirror reflection of the first half.

English alphabet

You are given a string s. Check if the string is "s-palindrome".

Input

The only line contains the string s (1 ≤ |s| ≤ 1000) which consists of only English letters.

Output

Print "TAK" if the string s is "s-palindrome" and "NIE" otherwise.

Examples

Input

oXoxoXo

Output

TAK

Input

bod

Output

TAK

Input

ER

Output

NIE
#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
map<char,char> mp;
int main() {
	mp['A']='A';
	mp['b']='d';
	mp['d']='b';//樣例已知
	mp['H']='H';
	mp['I']='I';
	mp['M']='M';
	mp['O']='O';
	mp['o']='o';
	mp['p']='q';//看圖得知
	mp['q']='p';
	mp['T']='T';
	mp['U']='U';
	mp['V']='V';
	mp['v']='v';
	mp['W']='W';
	mp['w']='w';
	mp['X']='X';
	mp['x']='x';
	mp['Y']='Y';

	string s;
	cin>>s;
	int len=s.size();
	for(int i=0; i<=len/2; i++) {//前後比對是否“對稱”即可
		if(mp[s[i]]!=s[len-1-i]) {
			cout<<"NIE\n";
			return 0;
		}
	}
	cout<<"TAK\n";
	return 0;
}

如果嫌輸入的“匹配”太多,下例也是一種很好的選擇  

          

 

 

 

 

Exponential notation(數據的處理——情況考慮)

You are given a positive decimal number x.

Your task is to convert it to the "simple exponential notation".

Let x = a·10b, where 1 ≤ a < 10, then in general case the "simple exponential notation" looks like "aEb". If b equals to zero, the part "Eb" should be skipped. If a is an integer, it should be written without decimal point. Also there should not be extra zeroes in a and b.

Input

The only line contains the positive decimal number x. The length of the line will not exceed 106. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other.

Output

Print the only line — the "simple exponential notation" of the given number x.

Examples

Input

16

Output

1.6E1

Input

01.23400

Output

1.234

Input

.100

Output

1E-1

Input

100.

Output

1E2

 

思路:分別找小數點的位置、從左數第一個非零數的位置、從右數第一個非零數的位置

  • 若小數點沒有——放到字符串最後
  • 若左數第一位不爲零的數沒有則直接輸出0
  • 輸出第一位,若只有一位不輸出’.‘
  • 做差判斷科學計數法中的指數情況,指數爲零不輸出’E‘
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
int main() {
	string s;
	getline(cin,s);
	int len=s.size();
	int l=-1,r=len-1,n=0,pos=-1;
	for(int i=0; i<len; i++) { //求小數點的位置
		if(s[i]=='.') {
			pos=i;
			break;
		}
	}
	if(pos==-1) pos=len;//如果沒有小數點放在最後
	for(int i=0; i<len; i++) { //找左邊第一個不是0的數的位置
		if(s[i]!='0'&&s[i]!='.') {
			l=i;
			break;
		}
	}
	for(int i=len-1; i>=0; i--) { //找右邊第一個不是0的數的位置
		if(s[i]!='0'&&s[i]!='.') {
			r=i;
			break;
		}
	}
	if(l==-1) cout<<"0\n";
	else {
		if(pos>l)	n=pos-l-1; //指數爲正數
		else n=pos-l; //指數爲負數
		cout<<s[l];
		if(l!=r) cout<<'.';//若只有一位不輸出'.'
		for(int i=l+1; i<=r; i++)
			if(s[i]!='.') cout<<s[i];//原來的'.'不輸出
		if(n!=0) cout<<'E'<<n<<endl;
	}
	return 0;
}

 

 

 

 

 

Night at the Museum(貪心)

Grigoriy, like the hero of one famous comedy film, found a job as a night security guard at the museum. At first night he received embosser and was to take stock of the whole exposition.

Embosser is a special devise that allows to "print" the text of a plastic tape. Text is printed sequentially, character by character. The device consists of a wheel with a lowercase English letters written in a circle, static pointer to the current letter and a button that print the chosen letter. At one move it's allowed to rotate the alphabetic wheel one step clockwise or counterclockwise. Initially, static pointer points to letter 'a'. Other letters are located as shown on the picture:

After Grigoriy add new item to the base he has to print its name on the plastic tape and attach it to the corresponding exhibit. It's not required to return the wheel to its initial position with pointer on the letter 'a'.

Our hero is afraid that some exhibits may become alive and start to attack him, so he wants to print the names as fast as possible. Help him, for the given string find the minimum number of rotations of the wheel required to print it.

Input

The only line of input contains the name of some exhibit — the non-empty string consisting of no more than 100 characters. It's guaranteed that the string consists of only lowercase English letters.

Output

Print one integer — the minimum number of rotations of the wheel, required to print the name given in the input.

Examples

Input

zeus

Output

18

Input

map

Output

35

Input

ares

Output

34

Note

 

To print the string from the first sample it would be optimal to perform the following sequence of rotations:

  1. from 'a' to 'z' (1 rotation counterclockwise),
  2. from 'z' to 'e' (5 clockwise rotations),
  3. from 'e' to 'u' (10 rotations counterclockwise),
  4. from 'u' to 's' (2 counterclockwise rotations).

In total, 1 + 5 + 10 + 2 = 18 rotations are required.

 

 注意區分何時貪心何時dp
因爲每次旋轉起點都取決於上一次地址(輸入字符串即已知)故可用貪心

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int main() {
	string s;
	int ans=0;
	cin>>s;
	char cur='a';
	for(int i=0; i<s.size(); i++) {
		ans+=min(abs(s[i]-cur),26-abs(s[i]-cur));
		cur=s[i];
	}
	cout<<ans;
	return 0;
}

 

 

 

 

 

Links and Pearls(情況判斷)

A necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one.

You can remove a link or a pearl and insert it between two other existing links or pearls (or between a link and a pearl) on the necklace. This process can be repeated as many times as you like, but you can't throw away any parts.

Can you make the number of links between every two adjacent pearls equal? Two pearls are considered to be adjacent if there is no other pearl between them.

Note that the final necklace should remain as one circular part of the same length as the initial necklace.

Input

The only line of input contains a string ss (3≤|s|≤1003≤|s|≤100), representing the necklace, where a dash '-' represents a link and the lowercase English letter 'o' represents a pearl.

Output

Print "YES" if the links and pearls can be rejoined such that the number of links between adjacent pearls is equal. Otherwise print "NO".

You can print each letter in any case (upper or lower).

Examples

Input

-o-o--

Output

YES

Input

-o---

Output

YES

Input

-o---o-

Output

NO

Input

ooo

Output

YES
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int main() {
	string s;
	cin>>s;
	int cnt0=0,cnt1=0;
	for(int i=0; i<s.size(); i++) {
		if(s[i]=='-') cnt1++;
		else if(s[i]=='o') cnt0++;
	}
	if(cnt1==0||cnt0==0||cnt0==1) cout<<"YES\n";
	else if(cnt1%cnt0==0) cout<<"YES\n";
	else cout<<"NO\n";
	return 0;
}

 

 

 

 

 

Marlin(數據範圍->思維)

The city of Fishtopia can be imagined as a grid of 44 rows and an odd number of columns. It has two main villages; the first is located at the top-left cell (1,1)(1,1), people who stay there love fishing at the Tuna pond at the bottom-right cell (4,n)(4,n). The second village is located at (4,1)(4,1) and its people love the Salmon pond at (1,n)(1,n).

The mayor of Fishtopia wants to place kk hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.

A person can move from one cell to another if those cells are not occupied by hotels and share a side.

Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?

Input

The first line of input contain two integers, nn and kk (3≤n≤993≤n≤99, 0≤k≤2×(n−2)0≤k≤2×(n−2)), nn is odd, the width of the city, and the number of hotels to be placed, respectively.

Output

Print "YES", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print "NO".

If it is possible, print an extra 44 lines that describe the city, each line should have nn characters, each of which is "#" if that cell has a hotel on it, or "." if not.

Examples

Input

7 2

Output

YES
.......
.#.....
.#.....
.......

Input

5 3

Output

YES
.....
.###.
.....
.....

這道題不存在NO的情況,都是YES。所以如果K是偶數就先放左邊,如果K是奇數就從中間開始放;
題意:
有一個城市有4行n列,n是奇數,有一個村莊在(1,1),村民的活動地點是(4,n);
有一個村莊在(4,1),村民的活動地點是(1,n);
現在要修建k個賓館,不能修建在邊界上,問能否給出一種安排方案使得兩個村莊的村民到他們各自的活動地點的最短路的條數相等。
思路:
畫了幾個實例就應該知道,無論n和k是多少,都可以構建出合理的方案,所以全是YES。
如果k爲偶數,那麼就上下對稱,這個比較好構造;當k爲奇數,我採用的是首先把第二排從中間開始向兩邊填滿,然後第三排則是從中間一格的兩邊開始填。

#include<bits/stdc++.h>
using namespace std;
#define eps 1e-6
char mp[5][105];
int main() {
	int col,k,l,r;
	memset(mp,'.',sizeof mp);
	cin>>col>>k;
	if(k%2) {
		mp[2][col/2+1]='#';
		k--;
		l=col/2,r=col/2+2;
		while(l>1&&r<col&&k) {
			mp[2][l]=mp[2][r]='#';
			l--,r++;
			k-=2;
		}
		l=col/2,r=col/2+2;
		while(l>1&&r<col&&k) {
			mp[3][l]=mp[3][r]='#';
			l--,r++;
			k-=2;
		}
	} else {
		for(int j=2; j<=k/2+1; j++)
			mp[2][j]=mp[3][j]='#';
	}
	cout<<"YES\n";
	for(int i=1; i<=4; i++) {
		for(int j=1; j<=col; j++)
			cout<<mp[i][j];
		cout<<endl;
	}
	return 0;
}

 

 

 

 

 

Sasha and Sticks

It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends.

Today he invented one simple game to play with Lena, with whom he shares a desk. The rules are simple. Sasha draws n sticks in a row. After that the players take turns crossing out exactly k sticks from left or right in each turn. Sasha moves first, because he is the inventor of the game. If there are less than k sticks on the paper before some turn, the game ends. Sasha wins if he makes strictly more moves than Lena. Sasha wants to know the result of the game before playing, you are to help him.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 1018, k ≤ n) — the number of sticks drawn by Sasha and the number k — the number of sticks to be crossed out on each turn.

Output

If Sasha wins, print "YES" (without quotes), otherwise print "NO" (without quotes).

You can print each letter in arbitrary case (upper of lower).

Examples

Input

1 1

Output

YES

Input

10 4

Output

NO

Note

In the first example Sasha crosses out 1 stick, and then there are no sticks. So Lena can't make a move, and Sasha wins.

In the second example Sasha crosses out 4 sticks, then Lena crosses out 4 sticks, and after that there are only 2 sticks left. Sasha can't make a move. The players make equal number of moves, so Sasha doesn't win.

 數據範圍要切記!!!

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#define ll long long
using namespace std;
int main() {
	ll n,k;
	cin>>n>>k;
	ll turn=n/k;
	if(turn%2) cout<<"YES\n";
	else cout<<"NO\n";
	return 0;
}

 

 

 

 

 

 

Petya and Exam(思維)

It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..

There is a glob pattern in the statements (a string consisting of lowercase English letters, characters "?" and "*"). It is known that character "*" occurs no more than once in the pattern.

Also, n query strings are given, it is required to determine for each of them if the pattern matches it or not.

Everything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning.

A pattern matches a string if it is possible to replace each character "?" with one good lowercase English letter, and the character "*" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string.

The good letters are given to Petya. All the others are bad.

Input

The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad.

The second line contains the pattern — a string s of lowercase English letters, characters "?" and "*" (1 ≤ |s| ≤ 105). It is guaranteed that character "*" occurs in s no more than once.

The third line contains integer n (1 ≤ n ≤ 105) — the number of query strings.

n lines follow, each of them contains single non-empty string consisting of lowercase English letters — a query string.

It is guaranteed that the total length of all query strings is not greater than 105.

Output

Print n lines: in the i-th of them print "YES" if the pattern matches the i-th query string, and "NO" otherwise.

You can choose the case (lower or upper) for each letter arbitrary.

Examples

Input

ab
a?a
2
aaa
aab

Output

YES
NO

Input

abc
a?a?a*
4
abacaba
abaca
apapa
aaaaax

Output

NO
YES
NO
YES

Note

In the first example we can replace "?" with good letters "a" and "b", so we can see that the answer for the first query is "YES", and the answer for the second query is "NO", because we can't match the third letter.

Explanation of the second example.

  • The first query: "NO", because character "*" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string "ba", in which both letters are good.
  • The second query: "YES", because characters "?" can be replaced with corresponding good letters, and character "*" can be replaced with empty string, and the strings will coincide.
  • The third query: "NO", because characters "?" can't be replaced with bad letters.
  • The fourth query: "YES", because characters "?" can be replaced with good letters "a", and character "*" can be replaced with a string of bad letters "x".
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define eps 1e-6
int vis[200];
int main() {
	string good,bef,aft;
	int n;
	cin>>good;
	for(int i=0; i<good.size(); i++)
		vis[good[i]]=1;//標記“好字母”
	cin>>bef;
	int len1=bef.size();
	int havestar=0;
	for(int i=0; i<len1; i++) {//檢查是否含*
		if(bef[i]=='*') {
			havestar=1;
			break;
		}
	}
	int flag,nowpos;
	cin>>n;
	while(n--) {
		flag=0,nowpos=0;
		cin>>aft;
		int len2=aft.size();
		if(havestar==0&&len1!=len2) { //若不含*且長度不等 No
			cout<<"No\n";
			continue;
		} else if(havestar==1&&len1-1>len2) { //若含*但長度-1(減掉*的長度,其最多出現一次) 大於len2 No
			cout<<"No\n";
			continue;
		}
		for(int i=0; i<len1; i++) {
			if(bef[i]=='?') {  //如果是問號但aft中不是好字母 No 
				if(vis[aft[i+nowpos]]==0)//爲防止之前匹配過'*',增設變量nowaft爲二者長度之差,以保證匹配對應 
					flag=1;
			}
			else if(bef[i]=='*') { //如果是*從此開始到i+len2-len1 必須都是壞字母 
				int tmp=len2-len1;
				nowpos=tmp;
				for(int j=i; j<=i+tmp; j++){
					if(vis[aft[j]]){
						flag=1;
						break;
					}
				}
			} else {  //爲防止之前匹配過'*',增設變量nowaft爲二者長度之差,以保證匹配對應 
				if(bef[i]!=aft[i+nowpos])
					flag=1;
			}
			if(flag) break;
		}
		if(flag) cout<<"NO\n";
		else cout<<"YES\n";
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章