CodeChef Online Judge Solution Rupsa and the Game Problem Code: RGAME – Solution

CodeChef Online Judge Solution CodeChef-Beginner Solution

CodeChef Online Judge Solution Rupsa and the Game Problem Code: RGAME – SolutionCodeChef Online Judge Solution Subsequence Equality | Beginner

CodeChef Problem Link – https://www.codechef.com/problems/RGAME

Problem Name: Rupsa and the Game solution Codechef
Problem Number : CodeChef- Rupsa and the Game RGAME
Online Judge : CodeChef Online Judge Solution
Category: Beginner
Solution Language : C,C plus plus, java, python, c#(c sharp)

CodeChef Online Judge Solution Subsequence Equality Problem Code: LIKECS01 - Solution

CodeChef Solution Rupsa and the Game Code in C / Codechef RGAME solution in c language:

#include<stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

typedef long long int ll;
#define MOD 1000000007
int a[100005];
ll store[100005];
ll two[100005];

ll power(ll a)
{
if (!a || a < 0)
{
return 1;
}
else if (a % 2)
{
return (2 * power(a / 2) * power(a / 2)) % MOD;
}
else
{
return (power(a / 2) * power(a / 2)) % MOD;
}
}

int main()
{
int T, N, i, j;
ll sum, add;
scanf("%d", &T);
while (T--)
{
sum = 0;
scanf("%d", &N);
N += 1;
for (i = 0; i < N; i++)
{
scanf("%d", &a[i]);
two[i + 1] = power(i);
}
two[0] = 1;
for (i = N - 1; i > 0; i--)
{
store[i] = (store[i + 1] + (a[i] * two[N - i]) % MOD) % MOD;
}
for (i = 0; i < N - 1; i++)
{
add = a[i];
add = (two[i] * add) % MOD;
add = (add * store[i + 1]) % MOD;
sum = sum + add;
}
printf("%lldn", (sum * 2) % MOD);
}
return 0;
}

CodeChef Solution Rupsa and the Game Code / CodeChef RGAME solution in CPP:

#include <cstdio>
using namespace std;
#define get getchar_unlocked
#define MOD 1000000007
typedef unsigned long long ull;


int pwr[100000];

// Fast I/O Operations

ull scan()
{
int n = 0, ch = get();
while (ch < '0' || ch > '9')
ch = get();
while (ch >= '0' && ch <= '9') {
n = n * 10 + ch - '0';
ch = get();
}
return n;
}

int main()
{
ull T = scan(), N, Ai, sum, extra;

pwr[0] = 1;
for (int i = 1; i < 100000; i++)
pwr[i] = (pwr[i - 1] << 1) % MOD;

while(T--){
N = scan();
Ai = scan();
extra = Ai * 2;
sum = 0;
for (int i = 1; i <= N; i++){
Ai = scan();
sum = (sum * 2 + Ai * extra) % MOD;
extra = (extra + pwr[i] * Ai) % MOD;
}
printf("%llun", sum);
}
return 0;
}

CodeChef Solution Rupsa and the Game Code / CodeChef RGAME solution in Java:

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class Main
{

static final int MOD = (int)1e9 + 7;

int sum = 0;

void bruteSolve(String s, int[] a)
{
int idx1 = s.charAt(0) - '0';
int idx2 = s.charAt(s.length() - 1) - '0';
if (s.length() == a.length)
{

for (int i = 1; i < s.length(); i++)
sum += a[s.charAt(i) - '0'] * a[s.charAt(i - 1) - '0'];
System.out.println(s);
return;
}
int idx = s.length();
if (idx == idx1 || idx == idx2)
throw new RuntimeException();

// sum += a[idx1] * a[idx];
bruteSolve(s + idx, a);
// sum += a[idx2] * a[idx];
bruteSolve(idx + s, a);
}

int solve(int[] a)
{
int sz = 1;
int def = 0;
int sum = a[0] + a[0];

for (int i = 1; i < a.length; i++)
{
def = (int) ((2 * def + 1L * sum * a[i]) % MOD);
sz <<= 1;
if (sz >= MOD)
sz -= MOD;
sum = (int)((sum + 1L * sz * a[i]) % MOD);
}
return def;
}

public static void main(String[] args) throws IOException
{
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int tests = InputReader.nextInt();
while (tests-- != 0)
{
int[] a = new int[InputReader.nextInt() + 1];
for (int i = 0; i < a.length; i++)
a[i] = InputReader.nextInt();
out.println(new Main().solve(a));
}
out.close();
}

private static class InputReader
{

static java.io.BufferedInputStream in = new java.io.BufferedInputStream(System.in);
static byte[] buf = new byte[1 << 20];
static int bytesRead;
static int pos = -1;

static
{
try {
bytesRead = in.read(buf);
}
catch (java.io.IOException e)
{
}
}

static int nextInt() throws java.io.IOException
{
while (++pos < bytesRead && (buf[pos] < 48 || buf[pos] > 57));//parsing unwanted data
while (pos == bytesRead)
{
bytesRead = in.read(buf);
pos = -1;
while (++pos < bytesRead && (buf[pos] < 48 || buf[pos] > 57));
}
pos--;
int n = 0;
while (++pos < bytesRead && buf[pos] > 47 && buf[pos] < 58)
n = n * 10 + buf[pos] - '0';
while (pos == bytesRead)
{
bytesRead = in.read(buf);
pos = -1;
while (++pos < bytesRead && buf[pos] > 47 && buf[pos] < 58)
n = n * 10 + buf[pos] - '0';
}
return n;
}
}
}

CodeChef Solution Rupsa and the Game Code / CodeChef RGAME solution in  Python:

mod = 10**9 + 7

def rgame(a, n):
s = 2 * a[0]
ans = 0
p = 1
for i in xrange(1, n + 1):
p = (2 * p) % mod
ans = (2 * ans + s * a[i]) % mod
s = (s + p * a[i]) % mod
return ans

t = int(raw_input())
for i in xrange(t):
n = int(raw_input())
a = map(int, raw_input().split())
print rgame(a, n)

CodeChef Solution Rupsa and the Game Problem Code: RGAME Code / CodeChef RGAME solution in  C# (C Sharp):

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
static void solve(StreamReader reader, StreamWriter writer)
{
int tests = reader.ReadInt();
while (tests-- > 0)
{
writer.Flush();
int n = reader.ReadInt() + 1;
var numbers = new List<long>();
while (n-- > 0)
{
numbers.Add(reader.ReadInt());
}
long result = 0;
long run = 0;
long modulo = 1000000007;

n = numbers.Count - 1;
var cur = new long[n];
cur[0] = 1;
for (int i = 1; i < n; ++i)
{
cur[i] = (cur[i - 1] << 1) % modulo;
}
cur[0] = 2;
for (int i = 1; i <= n; ++i)
{
run = (run + (numbers[i - 1] * cur[i - 1]) % modulo) % modulo;
result = (result << 1) % modulo;
result = (result + (run * numbers[i]) % modulo) % modulo;
}
writer.WriteLine(result);
}
}


static void Main(string[] args)
{
using (var writer = new StreamWriter(Console.OpenStandardOutput()))
{
using (var reader = new StreamReader(
#if CODECHIEF_LOCAL
"input.txt"
#else
Console.OpenStandardInput()
#endif
))
{
solve(reader, writer);
}
}
}

}

#region extensions
static class IOExtensions
{
public static string ReadString(this StreamReader reader)
{
return reader.ReadToken();
}

public static int ReadInt(this StreamReader reader)
{
return int.Parse(reader.ReadToken());
}

public static long ReadLong(this StreamReader reader)
{
return long.Parse(reader.ReadToken());
}

static Queue<string> buffer = new Queue<string>(100);

static string ReadToken(this StreamReader reader)
{
while (buffer.Count == 0)
{
reader.ReadLine().Split(new[] { ' ', 't' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach((t) =>
{
buffer.Enqueue(t);
});
}
return buffer.Dequeue();
}
}
#endregion

Demonstration:

Just implement this in coding. Since having any problem just put a comment below. Thanks

Tags: CodeChef Online Judge Solution, CodeChef OJ Solution list, CodeChef Problems Solution, CodeChef solver, URI all problem solution list, Codechef Rupsa and the Game code in C, CodeChef RGAME code in C++, CodeChef Rupsa and the Game  solution in C, Codechef solution, CodeChef RGAME solution in C,CodeChef RGAME solution in C++-CPP,CodeChef RGAME solution in C# (C sharp),CodeChef RGAME solution in Java,CodeChef RGAME solution in Python,

Leave a Reply

Your email address will not be published.