Se7en CodeChef Solution

Problem -Se7en CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Se7en CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;

class Seven{
    public:
    const int M = 1337;
    int t[7][101];
    int c[7][101];
    int p10[101];
    char s[101];

    void funct()
    {
        for(int k = 1; k < 7; ++k) 
        t[k][0] = c[k][0] = 1;
	t[0][0] = c[0][0] = -1;
	p10[0] = 1;
	for(int len = 1; len <= 100; ++len) 
    {
		p10[len] = (10 * p10[len - 1]) % 7;
		for(int md = 0; md < 7; ++md) 
        {
			t[md][len] = 0;
			c[md][len] = 1;
			for(int d = 0; d <= 9; ++d) 
            {
				if(d != 7) 
                {
					t[md][len] += c[md][len] * t[(p10[len - 1] * d + md) % 7][len - 1];
					c[md][len] *= c[(p10[len - 1] * d + md) % 7][len - 1];
				} 
                else if(len == 1) 
                {
					t[md][len] += -c[md][len];
					c[md][len] *= -1;
				}
			}
			t[md][len] = (t[md][len] % M + M) % M;
		}
	}
	int d;
	scanf("%d", &d);
	while(d--) {
		scanf("%s", s);
		int len = strlen(s);
		int p = -1;
		int cc = -1;
		int md = 0;
		reverse(s, s + len);
		for(int i = len - 1; i >= 0; --i) {
			for(int d = 0; d < s[i] - '0'; ++d) {
				if(d != 7) {
					p += cc * t[(md + d * p10[i]) % 7][i];
					cc *= c[(md + d * p10[i]) % 7][i];
				} else if (i == 0) {
					p += -cc;
					cc *= -1;
				}
			}
			if(s[i] == '7' && i) {
				if(s[0] % 2) {
					p -= cc;
				}
				break;
			}
			md = (md + (s[i] - '0') * p10[i]) % 7;
		}
		printf("%d\n", (p % M + M) % M + 1);
	}
    }
    
};

int main() {
    Seven s;
    s.funct();
	return 0;
}

Se7en CodeChef Solution in C++14

#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
const int M = 1337;
 
int t[7][101];
int c[7][101];
int p10[101];
 
char s[101];
 
int main() {
	for(int k = 1; k < 7; ++k) t[k][0] = c[k][0] = 1;
	t[0][0] = c[0][0] = -1;
	p10[0] = 1;
	for(int len = 1; len <= 100; ++len) {
		p10[len] = (10 * p10[len - 1]) % 7;
		for(int md = 0; md < 7; ++md) {
			t[md][len] = 0;
			c[md][len] = 1;
			for(int d = 0; d <= 9; ++d) {
				if(d != 7) {
					t[md][len] += c[md][len] * t[(p10[len - 1] * d + md) % 7][len - 1];
					c[md][len] *= c[(p10[len - 1] * d + md) % 7][len - 1];
				} else if(len == 1) {
					t[md][len] += -c[md][len];
					c[md][len] *= -1;
				}
			}
			t[md][len] = (t[md][len] % M + M) % M;
		}
	}
	int d;
	std::cin >> d;
	while(d--) {
		std::cin >> s;
		int len = strlen(s);
		int p = -1;
		int cc = -1;
		int md = 0;
			reverse(s, s + len);
		for(int i = len - 1; i >= 0; --i) {
			for(int d = 0; d < s[i] - '0'; ++d) {
				if(d != 7) {
					p += cc * t[(md + d * p10[i]) % 7][i];
					cc *= c[(md + d * p10[i]) % 7][i];
				} else if (i == 0) {
					p += -cc;
					cc *= -1;
				}
			}
			if(s[i] == '7' && i) {
				if(s[0] % 2) {
					p -= cc;
				}
				break;
			}
			md = (md + (s[i] - '0') * p10[i]) % 7;
		}
		int f = (p % M + M) % M + 1;
		
		std::cout << f << std::endl;
	}
}

Se7en CodeChef Solution in C

#include <stdio.h>


#define MAX 105
#define OST 1337

int s[MAX][7];
int f[MAX][7];
int ost[6]={1,3,2,6,4,5};
char niz[MAX];

int main()
{
	int i,j,m,k,t,u,d,v;

	s[1][5]=s[1][6]=1;

	for(k=1;k<MAX-1;k++)
		for(m=0;m<7;m++)
			s[k+1][m]=s[k][m]^s[k][(m+3*ost[k%6])%7]^s[k][(m+4*ost[k%6])%7]^s[k][(m+5*ost[k%6])%7]^s[k][(m+6*ost[k%6])%7];

	for(m=0;m<7;m++)
	{
		u=0;
		t=1;
		for(i=0;i<10;i++)
		{
			if (i==7||(i+m)%7==0)
				t=-t;
			u+=t;
//
			if (u<0)
				u+=OST;
			if (u>=OST)
				u-=OST;
//
		}
		f[1][m]=u;
	}

	for(k=1;k<MAX-1;k++)
		for(m=0;m<7;m++)
		{
			u=0;
			for(i=0;i<10;i++)
			{
				if (i==7)
					continue;
				t=1;
				for(j=0;j<i;j++)
					if (j!=7)
						t^=s[k][(m+j*ost[k%6])%7];
				u+=(2*t-1)*f[k][(m+i*ost[k%6])%7];
				if (u>=OST)
					u-=OST;
				if (u<0)
					u+=OST;
			}
			f[k+1][m]=u;
		}


	scanf("%d",&v);

	while (v--)
	{
		scanf("%s",niz);
		for(d=0;niz[d];d++);
		if (d>MAX)
			for(;;);
		u=-1;
		t=-1;;
		m=0;
		for(i=0;i<d;i++)
		{
			if (i==d-1)
			{
				for(j=0;j<niz[i]-'0';j++)
				{
					if (j==7||(j+m)%7==0)
						t=-t;
					u+=t;

					if (u<0)
						u+=OST;
					if (u>=OST)
						u-=OST;
				}
				continue;
			}
			for(j=0;j<niz[i]-'0';j++)
			{
				if (j==7)
					continue;
				u+=t*f[d-i-1][(m+j*ost[(d-i-1)%6])%7];
				t*=1-2*s[d-i-1][(m+j*ost[(d-i-1)%6])%7];

				if (u<0)
					u+=OST;
				if (u>=OST)
					u-=OST;
			}
			m+=j*ost[(d-i-1)%6];
			m%=7;
			if (niz[i]=='7')
			{
				if (((niz[d-1]-'0')&1))
					u-=t;

				if (u<0)
					u+=OST;
				if (u>=OST)
					u-=OST;

				break;
			}
		}
		printf("%d\n",u+1);
	}
}

Se7en CodeChef Solution in JAVA

import java.util.*;

public class Main
{
	static final int MAX = 100;
	static final int MOD = 1337;
	static class Pair
	{
		static final Pair REV = new Pair(-1, -1);
		static final Pair FWD = new Pair(1, 1);
		int change; int dir;
		public Pair(int change, int dir)
		{
			this.change = change;
			this.dir = dir;
		}
		public void append(Pair pair)
		{
			change = (MOD+(change+dir*pair.change)%MOD)%MOD;
			dir *= pair.dir;
		}
	}
	static int[] pow10 = new int[MAX];
	static Pair[][] memo = new Pair[7][MAX];
	static Pair solve(int mod, int digits)
	{
		if(memo[mod][digits]==null)
		{
			Pair res = new Pair(0, 1);
			if(digits==0)
			{
				if(mod==0)
					res.append(Pair.REV);
				else
					res.append(Pair.FWD);
			}
			else
			{
				for(int i=0;i<10;i++)
				{
					if(i==7)
					{
						if(digits==1)
							res.append(Pair.REV);
					}
					else
					{
						res.append(solve(
							(mod+i*pow10[digits-1])%7, digits-1));
					}
				}
			}
			memo[mod][digits] = res;
		}
		return memo[mod][digits];
	}
	
	static int solve(String number)
	{
		Pair res = new Pair(0, -1);
		int mod = 0, lastDigit = number.charAt(number.length()-1)-'0';
		for(int i=0;i<number.length();i++)
		{
			int digit = number.charAt(i)-'0';
			int remaining = number.length()-i-1;
			for(int j=0;j<digit;j++)
			{
				if(j==7)
				{
					if(remaining==0)
						res.append(Pair.REV);
				}
				else
				{
					res.append(solve((mod+j*pow10[remaining])%7,remaining));
				}
			}
			if(digit==7)
			{
				if(remaining>0 && lastDigit%2==1)
					res.append(Pair.REV);
				break;
			}
			mod = (mod+pow10[remaining]*digit)%7;
		}
		int ret = res.change;
		if(ret==0)
			ret += MOD;
		return ret;
	}
	
	
	public static void main(String[] args)
	{
		pow10[0] = 1;
		for(int i=1;i<MAX;i++)
			pow10[i] = (pow10[i-1]*10)%7;
		
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0)
			System.out.println(solve(sc.next()));
	}
}

Se7en CodeChef Solution in C#

using System;
using System.Collections.Generic;
using System.Text;

namespace Monster
{
    class ChefSeven
    {

        static item[,] a = new item[105, 7];
        static int [] mods = new int[105];

        static void Main(string[] args)
        {
            //SolveBrute();
            PreProcess();
            //for (int i = 300; i < 1000; i++)
            //{
            //    int val = Solve(toIntStr(i.ToString()));
            //    Console.WriteLine(i+ " = "+val);
            //}
            int numtest = int.Parse(Console.ReadLine().Trim());
            for (int i = 0; i < numtest; i++)
            {
                int[] val = toIntStr(Console.ReadLine().Trim());
                int sol = Solve(val);
                Console.WriteLine(sol);
            }
        }

        static int[] toIntStr(string str)
        {
            int[] arr = new int[str.Length];
            for (int i = 0; i < str.Length; i++)
                arr[i] = (int)(str[i] - '0');
            return arr;
        }
        static int Solve(int[] arg)
        {
            item val = new item(0, true);
            int len = arg.Length;
            int summod = 0;
            bool broken = false;
            for (int i = 0; i < len-1; i++)
            {
                int mdd = mods[len - i - 1];
                if (arg[i] <= 6)
                {
                    for (int j = 0; j < arg[i]; j++)
                    {
                        val = val.add(a[len - i - 1, (summod + mdd * j) % 7]);
                    }
                }
                else if(arg[i]==7)
                {
                    for (int j = 0; j < 7; j++)
                    {
                        val = val.add(a[len - i - 1, (summod + mdd * j) % 7]);
                    }
                    if (summod % 7 != 0)
                        val = val.add(new item(0, false));
                    if (arg[len - 1] % 2 != 0)
                    {
                        val = val.add(new item(1, false));
                    }
                    broken = true;
                    break;
                }
                else if (arg[i] == 8)
                {
                    for (int j = 0; j < 7; j++)
                    {
                        val = val.add(a[len - i - 1, (summod + mdd * j) % 7]);
                    }
                    if (summod % 7 != 0)
                        val = val.add(new item(0, false));
                   
                    if ((summod + (mods[len - i - 1]) * 8) % 7 != 0)
                        val = val.add(new item(0, false));
                }
                else if(arg[i] == 9)
                {
                    for (int j = 0; j < 7; j++)
                    {
                        val = val.add(a[len - i - 1, (summod + mdd * j) % 7]);
                    }
                    if (summod % 7 != 0)
                        val = val.add(new item(0, false));
                   
                    if ((summod + (mods[len - i - 1]) * 8) % 7 != 0)
                        val = val.add(new item(0, false));
                    val = val.add(a[len - i - 1, (summod + mdd * 8) % 7]);
                   
                }

                //if (arg[i] != 7)
                //{
                //    int mdd = mods[len - i -1];
                //    for (int j = 0; j < arg[i]; j++)
                //    {
                //       // summod += (mods[len - i - 1] ) % 7;
                //       // summod = summod % 7;

                //        if (j != 7)
                //            val = val.add(a[len - i - 1, (summod + mdd * j) % 7]);
                //        else
                //            if((summod + (mods[len - i - 1] )*8) % 7 ==0)
                //                val = val.add(new item(0, false));

                //    }
                    
                //}
                //else
                //{
                //    int mdd = mods[len - i - 1];
                //    for (int j = 0; j < 7; j++)
                //    {
                //        val = val.add(a[len - i - 1, (summod + mdd * j) % 7]);
                //    }
                //    if (summod != 0)
                //        val = val.add(new item(0, false));
                //    if ( arg[len - 1] % 2 != 0)
                //    {
                //        val = val.add(new item(1, false));
                //    }
                //    broken = true;
                //    break;
                //}
                summod += (mods[len - i - 1] * arg[i]) % 7;
                summod = summod % 7;
            }
            if (!broken)
            {
                for (int i = 0; i < arg[len - 1]; i++)
                {
                    val = val.add(a[0, (summod + i) % 7]);
                    if (i == 6 && summod!=0)
                        val = val.add(new item(0, false));
                }
            }
            return val.move;
        }

        static void SolveBrute()
        {
            item it = new item(0, true);
            for (int i = 1; i < 1001; i++)
            {
                if (!isRev(i))
                    it = it.add(new item(1, true));
                else
                    it = it.add(new item(1, false));
                Console.Write(i + " = " + it.move );
                Console.WriteLine("");
                //if (it.forward)
                //    Console.WriteLine("->");
                //else
                //    Console.WriteLine("<-");
            }
        }

        static bool isRev(int arg)
        {
            if (arg % 7 == 0)
                return true;
            string str = arg.ToString();
            for (int i = 0; i < str.Length; i++)
                if (str[i] == '7')
                    return true;
            return false;
        }

        static void PreProcess()
        {
            a[0, 0] = new item(1, true);
            a[0, 1] = new item(1, true);
            a[0, 2] = new item(1, true);
            a[0, 3] = new item(1, true);
            a[0, 4] = new item(1, true);
            a[0, 5] = new item(1, true);
            a[0, 6] = new item(1, false);

            a[1, 0] = new item(4, false);
            a[1, 1] = new item(8, true);
            a[1, 2] = new item(6, true);
            a[1, 3] = new item(4, true);
            a[1, 4] = new item(2, false);
            a[1, 5] = new item(1335, false);
            a[1, 6] = new item(1331, false);


            mods[0] = 1;
            for (int i = 1; i < 105; i++)
            {
                mods[i] = (mods[i - 1] * 10) % 7;
            }

            for (int i = 2; i < 105; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    item val = new item(0,true);
                    int [] mmod = new int[10];
                    for (int k = 0; k < 10; k++)
                        mmod[k] = (mods[i-1] * k + j) % 7;
                    for (int k = 0; k < 7; k++)
                    {
                        val = val.add(a[i-1,mmod[k]]);
                    }
                    //if (i == 1)
                     //   val = val.add(new item(1, true));
                    if(mmod[7]==0)
                        val = val.add(new item(0, false));
                    if(mmod[8] == 0)
                        val = val.add(new item(0,false));
                    for (int k = 8; k < 10; k++)
                    {
                        val = val.add(a[i - 1, mmod[k]]);
                    }
                    a[i, j] = val;
                }
            }
        }



    }

    class item
    {
        public int move = 0;
        public bool forward = true;
        const int onethree = 1337;
        public item(int move, bool forward)
        {
            this.move = move;
            this.forward = forward;
        }

        public item add(item arg)
        {
            int retmove = 0;
            bool retdir = false;

            if (this.forward)
            {
                retmove = (this.move + arg.move) % onethree;
                if (retmove == 0)
                    retmove = 1337;
                retdir = arg.forward;
                return new item(retmove, retdir);
            }
            else
            {
                retmove = (this.move - arg.move + onethree) % onethree;
                retdir = !arg.forward;
                if (retmove == 0)
                    retmove = 1337;
                return new item(retmove, retdir);
            }
        }

        public item reverseDir(item arg)
        {
            return new item(arg.move, !arg.forward);
        }
    }
}
Se7en CodeChef Solution Review:

In our experience, we suggest you solve this Se7en CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Se7en CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Se7en CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *