寒假训练1

C. Spell
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Maleficent got upset when she could not cancel the spell cast upon Aurora because it’s infinite and unbreakable. Luckily for us it’s not such a big problem because in our version of the story all spells are mathematically described and can be cancelled much easier.

The spell is determined by two positive integer numbers a and b. The process of cancelling the spell goes as follows:

1.Multiply numbers from a to b inclusive
2.Count the sum of digits of the result
If the sum of digits isn’t less then 10, go to step 2
To finish the cancellation rite, you need to say the resulting number. Please, help Maleficent to find it.

Input
The first line contains number a, the second — number b (1≤a≤b<10100000). Both numbers have no leading zeros.

Output
Output the resulting number.

Examples
inputCopy
1
5
outputCopy
3
inputCopy
6
8
outputCopy
3

打表找规律,不难看出差值大于8时,答案为9
其余情况,将a到b反复进行操作2,得出U={x,x+1,x+2…x+n,(n<7)}
U中元素进行操作1和2即可得出答案。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
typedef double db;
const int mod=1e9+7;
const int N=1e6+11;
const double eps=0.00000001;
string a,b;
string sub(string a,string b)
{
    string c;
    bool ok=0;
    int len1=a.length();
    int len2=b.length();
    int len=max(len1,len2);
    for(int i=len1;i<len;i++)
        a="0"+a;
    for(int i=len2;i<len;i++)
        b="0"+b;
    if(a<b)
    {
        string temp=a;
        a=b;
        b=temp;
        ok=1;
    }
    for(int i=len-1;i>=0;i--)
    {
        if(a[i]<b[i]) 
        {
            a[i-1]-=1;
            a[i]+=10;
        }
        char temp=a[i]-b[i]+'0';
        c=temp+c;
    }
    int pos=0;
    while(c[pos]=='0' && pos<len) pos++;
    if(pos==len) return "0"; 
    if(ok) return "-"+c.substr(pos);
    return c.substr(pos);
}
int main(){
	cin>>a>>b;
	int x,y;
	int lena=a.length();
	int lenb=b.length();
	x=y=0;
	for(int i=0;i<lena;++i){
		x=x+a[i]-'0';
		if(x>=10){
			x=x%10+x/10;
		}
	}
	if(a==b){
		printf("%d\n",x);
	}else{
		string c=sub(b,a);
		if(c.length()>=2||c[0]-'0'>=8) printf("9\n");
		else{
			int g=c[0]-'0'+1;
			int ans=1;
			for(int i=1;i<=g;++i){
				ans*=x;
				x++;
				if(x==10) x=1;
				while(ans>=10){
					ans=ans%10+ans/10;
				}
			}
			printf("%d\n",ans); 
		}
	}
	return 0;
}

A. Wooden Castle
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
IT is hiding in the abandoned house. To get there children need to open the door with a tricky lock. The lock is in fact a tree with n vertices, each of them is either white or black. The lock opens after every vertex is eliminated. For that purpose 2 operations are available:

Change the color of one vertex.
Set up a chain reaction eliminating a group of connected vertices of the same color. More formally, one vertex of color c can be chosen, then the group of vertices of color c, which can be reached from the chosen vertex through the vertices of color c, will be eliminated.
Naturally, the children want to get inside sooner, so they wonder how many operations are needed to open the lock.

Input
The first line contains n — the number of vertices in the graph. (1≤n≤200000). The next line contains a string s of the length n consisting of 0 and 1. If the i-th symbol of the string s is 0, than i-th vertex is white, otherwise — it is black. The next n−1 lines contain 2 integer numbers each ai and bi — the edges of the tree (1≤ai,bi≤n).

It is guaranteed that the edges form a tree.

Output
Output one number — the minimum number of operations needed to open the lock.

Example
inputCopy
4
1000
1 2
1 3
1 4
outputCopy
2
Note
In the first test case the lock can be open with 2 operations as follows:

Change the color of vertex 1 to white.
Set up a chain reaction from vertex 1, which will eliminate every vertex.

树形dp,dp[color][now],color表示当前点颜色,now为当前点。
dp[color][now]+=min(dp[color][son]-1,dp[!color][son]);
dp[!color][now]+=min(dp[!color][son]-1,dp[color][son]);

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
typedef double db;
const int mod=1e9+7;
const int N=1e6+11;
const double eps=0.00000001;
char s[N];
int n,a,b,dp[4][N];
vector<int>G[N];
void dfs(int x,int fa){
	dp[s[x]-'0'][x]=1;
	dp[!(s[x]-'0')][x]=2;
	for(int i=0;i<G[x].size();++i){
		int y=G[x][i];
		if(y==fa) continue;
		dfs(y,x);
		dp[s[x]-'0'][x]+=min(dp[s[x]-'0'][y]-1,dp[!(s[x]-'0')][y]);
		dp[!(s[x]-'0')][x]+=min(dp[!(s[x]-'0')][y]-1,dp[s[x]-'0'][y]);
		//cout<<dp[1][x]<<"!!!"<<dp[0][x]<<"!!!"<<x<<"!!!"<<y<<endl;
	}
}
int main(){
	cin>>n;
	scanf("%s",s+1);
	for(int i=1;i<n;++i){
		scanf("%d%d",&a,&b);
		G[a].push_back(b);
		G[b].push_back(a);
	}
	dfs(1,-1);
	cout<<min(dp[0][1],dp[1][1])<<endl;
	return 0;
}

H. Road building
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Sam is not the only one who builds roads. Today he met a man who does the same. They instantly hit it off and decided to play a game.

Right now they are building a rectangular part of the road n by m meters. Let’s imagine that the road is placed on a grid and consists of n×m cells. Before the start of the game no cell is build. Players take turns. The first player can choose any rectangle on the grid with total area less or equal to s, but no cell of that rectangle should be build. Then the player builds all the cells of the chosen rectangle. The second player does the same. The winner takes the last turn. Sam goes first. Please, help him understand if he will be the winner considering that both players intend to win and play optimally.

Input
The first line contains three integers n, m and s (1≤n,m≤1000, 1≤s≤n⋅m) — the sizes of the road and maximum area of the chosen rectangle.

Output
If Sam can win output “YES”. Otherwise output “NO”.

Example
inputCopy
1 4 2
outputCopy
YES

博弈题,对称操作,先手构造对称局面,后手怎么放置,先手就在对称位置放置。所以只需要判断先手是否能构造对称局势即可。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
typedef double db;
const int mod=1e9+7;
const int N=1e6+11;
const double eps=0.00000001;
/*inline void print(__int128 x){
	 if(x<0){
	 	putchar('-');
	 	x=-x;
	 }
	 if(x>9) print(x/10);
	 putchar(x%10+'0');
}*/
int main(){
	int n,m,s;
	cin>>n>>m>>s;
	if((n*m)&1) puts("YES");
	else if(!((n*m)&1)&&(n==1||m==1)&&s<2) puts("NO");
	else if(!((n*m)&1)&&(n&1)+(m&1)==1&&s<2) puts("NO");
	else if(!((n*m)&1)&&(n&1)+(m&1)==0&&s<4) puts("NO");
	else puts("YES");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值