cube

Problem Description

Mirko doesn’t like Latin homeworks so he made a bet with Slavko. Loser will be writing homeworks for both of them the entire month. Mirko wants to win so he designed this problem they could have something to bet on. 
At his desk he found a cube, with numbers 1 to 6 on its faces. Cube is shown on the picture. Additionally, sum of the numbers on opposing faces is equal to 7. That means that 6 is on the opposite face of 1, 5 on the opposite of 2 and 4 on the opposite face of 3. 
Mirko has put the cube in the upper left field of the matrix of R rows and C columns. The cube is initially oriented in a way that upper side is showing number 1, and side to the right number 3. 
Mirko now makes te following moves: 
1. He is rolling the cube to the right, until it reaches the last column
2. Then he rolls it down (to the next row) 
3. Now he rolls the cube to the left, until it reaches first column 
4. Like in step 2, he rolls it down (to the next row) 
Mirko is repeating these steps for as long as he can, i.e. as long as he can roll the cube in the next row. 
When a cube reaches some field, Mirko writes down the number on the top of the cube. In the end he sums all of the numbers he had written. 
Mirko made a bet with Slavko that he could calculate that sum without error. Help Slavko verifying Mirko’s solution! 

Input

First and only line of input contains two positive integers. R and C (1<=R,C<=100 000), matrix dimensions. 

Output

First and only line of input should contain the sum described in the task.

Sample Input

3 2
3 4
737 296

Sample Output

19
42
763532
题意:滚筛子,按照给定的矩阵矩阵执行。求一路滚过的最上面的所有的值的和
题解:模拟+找规律,注意要用64位。

#include<stdio.h>
#include<iostream>
#define ll __int64
using namespace std;
struct lmx{
ll l;
ll r;
ll u;
ll d;
ll f;
ll b;
};
lmx s,t;
int main()
{
ll m,n,step,i,j,sum,x,y,sr,time,te;
while(scanf("%I64d%I64d",&m,&n)!=EOF)
{
ll y=n%4;
if(y==0) y=4;
if(n%4==0)
{
te=n/4-1;
}
else te=n/4;
s.r=3;
s.l=4;
s.f=2;
s.b=5;
s.u=1;
s.d=6;
sr=s.f+s.b+s.r+s.l;
sum=s.u;
for(i=1;i<=m;i++)
{
for(j=1;j<y;j++)
{
if(i&1)
{
t.d=s.r;
t.u=s.l;
t.l=s.d;
t.r=s.u;
t.f=s.f;
t.b=s.b;
}
else
{
t.d=s.l;
t.u=s.r;
t.l=s.u;
t.r=s.d;
t.f=s.f;
t.b=s.b;
}
sum+=t.u;
s=t;
}
if(i!=m)
{
t.d=s.f;
t.u=s.b;
t.l=s.l;
t.r=s.r;
t.f=s.u;
t.b=s.d;
sum+=t.u;
s=t;
}
}
printf("%I64d\n",sum+te*sr*m);
}
return 0;
}

posted @ 2013-08-07 11:24  forevermemory  阅读(193)  评论(0编辑  收藏  举报