Codeforces Round #478 (Div. 2) B. Mancala

B. Mancala

Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.

Initially, each hole has aiai stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.

Note that the counter-clockwise order means if the player takes the stones from hole ii, he will put one stone in the (i+1)(i+1)-th hole, then in the (i+2)(i+2)-th, etc. If he puts a stone in the 1414-th hole, the next one will be put in the first hole.

After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.

Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.

Input

The only line contains 14 integers a1,a2,,a14a1,a2,…,a14 (0ai1090≤ai≤109) — the number of stones in each hole.

It is guaranteed that for any ii (1i141≤i≤14aiai is either zero or odd, and there is at least one stone in the board.

Output

Output one integer, the maximum possible score after one move.

Examples
input
Copy
0 1 1 0 0 0 0 0 0 7 0 0 0 0
output
Copy
4
input
Copy
5 1 1 1 1 0 0 0 0 0 0 0 0 0
output
Copy
8

题意:宝石棋,选一个洞拿出全部石子,从当前位置开始,向右一个一个放,每个洞放一个,第一行放到行末,第二行继续从左到右,放满之后从第一行第一个再次开始,最后每个洞里偶数石子累加即为答案。

思路:数据非常大,考虑除法,我们利用周期的思想,首先以当前位置为起点,取出a[i]个石子,一直填到第14个洞,说白了就是先补满第一个周期,剩余x=a[i]-(14-i),x/14得到填满周期轮的每个洞要填的,x%14得到填完所有周期,剩余的几个一个一个填就可以了.

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define exp 1e-8
#define mst(a,k) memset(a,k,sizeof(a))
using namespace std;
ll a[110],b[110],ans,tmp;
int main()
{
    ans=0;
    for(ll i=1;i<=14;i++)scanf("%lld",&a[i]);
    for(ll i=1;i<=14;i++)
    {
        for(ll j=1;j<=14;j++)b[j]=a[j];   //a是原数组,b是现在处理的数组
        ll k=a[i];
        b[i]=0;
        if(i+k>14)   //大于一周期的采用除法
        {
            for(ll j=i+1;j<=14;j++)b[j]++;  //补全第一周期
            k-=(14-i);
            ll mul=k/14;
            for(ll j=1;j<=14;j++)b[j]+=mul;   //填满所有满周期的
            k%=14;
            for(ll j=1;j<=k;j++)b[j]++;    //最后一轮不足一周期的
            tmp=0;
            for(ll j=1;j<=14;j++)
            {
                if(b[j]%2==0)tmp+=b[j];
            }
        }
        else
        {
            for(ll j=i+1;j<=i+k;j++)b[j]++;
            tmp=0;
            for(ll j=1;j<=14;j++)
            {
                if(b[j]%2==0)tmp+=b[j];
            }
        }
        if(tmp>ans)ans=tmp;
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/s540239976/article/details/80283662