Harry And Physical Teacher
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 564 Accepted Submission(s): 304
Problem Description
As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm.
Today, Harry's physical teacher set him a difficult problem: if a small ball moving with a speed V0 made a completely elastic collision with a car moving towards the same direction with a speed V(V<V0) , and the car far outweighs the ball, what was the speed of the small ball after the collision?
This problem was so difficult that Harry hasn't figure out how to solve it. Therefore, he asks you for help. Could you do him this favor?
Today, Harry's physical teacher set him a difficult problem: if a small ball moving with a speed V0 made a completely elastic collision with a car moving towards the same direction with a speed V(V<V0) , and the car far outweighs the ball, what was the speed of the small ball after the collision?
This problem was so difficult that Harry hasn't figure out how to solve it. Therefore, he asks you for help. Could you do him this favor?
Input
They are several test cases, you should process to the end of file.
There are two integers V and V0 for each test case. All the integers are 32-bit signed non-negative integers.
There are two integers V and V0 for each test case. All the integers are 32-bit signed non-negative integers.
Output
For each test case, just output one line that contains an integer indicate the speed of the small ball after the collision.
Sample Input
0 10
Sample Output
-10
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn=10010;
int main()
{
int t;
long long v0,v1;
while(scanf("%lld%lld",&v1,&v0)!=EOF){
printf("%lld\n",2*v1-v0);
}
return 0;
}
Harry And Dig Machine
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 814 Accepted Submission(s): 323
Problem Description
As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm.
Dumbledore, the headmaster of Hogwarts, is planning to construct a new teaching building in his school. The area he selects can be considered as an n*m grid, some (but no more than ten) cells of which might contain stones. We should remove the stones there in order to save place for the teaching building. However, the stones might be useful, so we just move them to the top-left cell. Taking it into account that Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore decides to let him do this job and wants it done as quickly as possible. Harry needs one unit time to move his dig machine from one cell to the adjacent one. Yet skilled as he is, it takes no time for him to move stones into or out of the dig machine, which is big enough to carry infinite stones. Given Harry and his dig machine at the top-left cell in the beginning, if he wants to optimize his work, what is the minimal time Harry needs to finish it?
Dumbledore, the headmaster of Hogwarts, is planning to construct a new teaching building in his school. The area he selects can be considered as an n*m grid, some (but no more than ten) cells of which might contain stones. We should remove the stones there in order to save place for the teaching building. However, the stones might be useful, so we just move them to the top-left cell. Taking it into account that Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore decides to let him do this job and wants it done as quickly as possible. Harry needs one unit time to move his dig machine from one cell to the adjacent one. Yet skilled as he is, it takes no time for him to move stones into or out of the dig machine, which is big enough to carry infinite stones. Given Harry and his dig machine at the top-left cell in the beginning, if he wants to optimize his work, what is the minimal time Harry needs to finish it?
Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m. (1≤n,m≤50) .
The next n line, each line contains m integer. The j-th number of ith line a[i][j] means there are a[i][j] stones on the jth cell of the ith line.( 0≤a[i][j]≤100 , and no more than 10 of a[i][j] will be positive integer).
For each test case, there are two integers n and m. (1≤n,m≤50) .
The next n line, each line contains m integer. The j-th number of ith line a[i][j] means there are a[i][j] stones on the jth cell of the ith line.( 0≤a[i][j]≤100 , and no more than 10 of a[i][j] will be positive integer).
Output
For each test case, just output one line that contains an integer indicate the minimal time that Harry can finish his job.
Sample Input
3 3 0 0 0 0 100 0 0 0 0 2 2 1 1 1 1
Sample Output
4 4
Source
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2510;
struct Node{
int x,y;
}A[maxn];
int cost[11][11];
int dp[1<<11][maxn];
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF){
int cnt=0,num;
for(i=1;i<=n;++i){
for(j=1;j<=m;++j){
scanf("%d",&num);
if(num>0){
A[cnt].x=i;A[cnt].y=j;cnt++;
}
}
}
if(cnt==0){
printf("0\n");
continue;
}
for(i=0;i<(1<<cnt);++i){
for(j=0;j<cnt;++j){
dp[i][j]=inf;
}
}
for(i=0;i<cnt;++i){
for(j=0;j<cnt;++j){
cost[i][j]=abs(A[i].x-A[j].x)+abs(A[i].y-A[j].y);
}
}
for(i=0;i<cnt;++i){
dp[1<<i][i]=A[i].x+A[i].y-2;
}
for(i=0;i<(1<<cnt);++i){
for(j=0;j<cnt;++j){
if(dp[i][j]==inf)continue;
for(k=0;k<cnt;++k){
if(i&(1<<k))continue;
dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+cost[j][k]);
}
}
}
int ans=inf;
for(i=0;i<cnt;++i){
ans=min(ans,dp[(1<<cnt)-1][i]+A[i].x+A[i].y-2);
}
printf("%d\n",ans);
}
return 0;
}
Harry And Math Teacher
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 506 Accepted Submission(s): 155
Problem Description
As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm.
In Hogwarts, there is a tall castle in which all teachers live. The castle is rather special. In every floor there are two doors, behind each of them there existing two stairs to the next floor’s two doors. And if you are at the i-th floor’s j-th door , then you can just go to the next floor from this door. However, something even more interesting (or we can say "magic") can happen to the stairs: sometimes they break into pieces (making it impossible to go to the next floor), and sometimes the fragments can joint together and become the whole stair again. Now suppose Harry is in the a-th floor (you know, Harry is the hero, so he lives in the teachers’ building somehow), and his math teacher b-th floor. Sometimes the math teacher will call Harry to his room. Facing these magic stairs, Harry gets puzzled how he can go to see the math teacher. Can you help Harry figure out how many ways exactly he can choose without going backwards? You can assume that the change of the stairs will not happen when Harry is on his way. Harry can begin at any doors in floor a and he can end at any doors in floor b. And as Harry want to arrive as soon as possible, so he can not go back to the past. And at the beginning all the stairs are intact. And the answer may be too large, you should output the answer mod 1000000007.
In Hogwarts, there is a tall castle in which all teachers live. The castle is rather special. In every floor there are two doors, behind each of them there existing two stairs to the next floor’s two doors. And if you are at the i-th floor’s j-th door , then you can just go to the next floor from this door. However, something even more interesting (or we can say "magic") can happen to the stairs: sometimes they break into pieces (making it impossible to go to the next floor), and sometimes the fragments can joint together and become the whole stair again. Now suppose Harry is in the a-th floor (you know, Harry is the hero, so he lives in the teachers’ building somehow), and his math teacher b-th floor. Sometimes the math teacher will call Harry to his room. Facing these magic stairs, Harry gets puzzled how he can go to see the math teacher. Can you help Harry figure out how many ways exactly he can choose without going backwards? You can assume that the change of the stairs will not happen when Harry is on his way. Harry can begin at any doors in floor a and he can end at any doors in floor b. And as Harry want to arrive as soon as possible, so he can not go back to the past. And at the beginning all the stairs are intact. And the answer may be too large, you should output the answer mod 1000000007.
Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m(2≤n≤50000,1≤m≤50000) in the first line, indicate the number of the castle’s layers and the number of queries. And the following m lines, each line contains three or four integers. If the first integer op equals 0, there are two integers a and b ( 1≤a<b≤n ) follow, indicate the position of Harry and math teacher. Otherwise, there are three integers x,y,z(1≤x<n,1≤y,z≤2) follow, it means that the stair between the xth floor’s yth door and the (x+1)th floor’s z-th door changes its state(if it is intact, then it breaks else it joints).
For each test case, there are two integers n and m(2≤n≤50000,1≤m≤50000) in the first line, indicate the number of the castle’s layers and the number of queries. And the following m lines, each line contains three or four integers. If the first integer op equals 0, there are two integers a and b ( 1≤a<b≤n ) follow, indicate the position of Harry and math teacher. Otherwise, there are three integers x,y,z(1≤x<n,1≤y,z≤2) follow, it means that the stair between the xth floor’s yth door and the (x+1)th floor’s z-th door changes its state(if it is intact, then it breaks else it joints).
Output
For each query, if op equals 0, you should output one line that contains an integer indicates the number of ways from
ath
floor to the
bth
floor.
Sample Input
3 1 0 1 3 3 2 1 2 1 1 0 1 3
Sample Output
8 6
Source
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define MOD 1000000007
using namespace std;
const int maxn=50010;
struct Node{
long long f[2][2];
}tree[maxn<<2];
void pushup(Node A,Node B,Node &C){
memset(C.f,0,sizeof(C.f));
for(int i=0;i<2;++i){
for(int k=0;k<2;++k){
if(A.f[i][k]){
for(int j=0;j<2;++j){
C.f[i][j]=(C.f[i][j]+A.f[i][k]*B.f[k][j])%MOD;
}
}
}
}
}
void build(int l,int r,int rt){
if(l==r){
for(int i=0;i<2;++i){
for(int j=0;j<2;++j){
tree[rt].f[i][j]=1;
}
}
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(tree[rt<<1],tree[rt<<1|1],tree[rt]);
}
void update(int x,int y,int z,int l,int r,int rt){
if(l==r){
tree[rt].f[y][z]^=1;
return ;
}
int mid=(l+r)>>1;
if(x<=mid)update(x,y,z,lson);
else update(x,y,z,rson);
pushup(tree[rt<<1],tree[rt<<1|1],tree[rt]);
}
Node query(int left,int right,int l,int r,int rt){
if(left==l&&r==right){
return tree[rt];
}
int mid=(l+r)>>1;
if(right<=mid)return query(left,right,lson);
if(left>mid)return query(left,right,rson);
Node ans;
pushup(query(left,mid,lson),query(mid+1,right,rson),ans);
return ans;
}
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF){
build(1,n-1,1);
while(m--){
int oper;
scanf("%d",&oper);
if(oper==0){
int a,b;
scanf("%d%d",&a,&b);
Node ans=query(a,b-1,1,n-1,1);
long long cnt=(ans.f[0][0]+ans.f[0][1]+ans.f[1][0]+ans.f[1][1])%MOD;
printf("%lld\n",cnt);
}
else {
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
y--;z--;
update(x,y,z,1,n-1,1);
}
}
}
return 0;
}