Codechef : Program to find if given very large number is divisible by 3 or not

Multiple of 3 Problem Code: MULTHREE



Consider a very long K-digit number N with digits d0, d1, ..., dK-1 (in decimal notation; d0 is the most significant and dK-1 the least significant digit). This number is so large that we can't give it to you on the input explicitly; instead, you are only given its starting digits and a way to construct the remainder of the number.

Specifically, you are given d0 and d1; for each i ≥ 2, di is the sum of all preceding (more significant) digits, modulo 10 — more formally, the following formula must hold: 

  
    

Determine if N is a multiple of 3.

Example

  3
  5 3 4
  13 8 1
  760399384224 5 1
  NO
  YES
  YES

Java Solution :

  //codechef very very long number Multiple of three
  // https://www.codechef.com/LRNDSA01/problems/MULTHREE

  import java.util.*;
  class MultipleOfThree
  {
    static void calc(long n,long a , long b) {
      long sum = a+b;
      long mod = sum%10;
      int i = 0;
      while(mod != 2) {
          if(mod == 0) break;
          if(i==n-2) break;
          sum += mod;
          mod = sum % 10;
          i++;
      }
      long remain = n - 2 - i;
      if(mod == 0) {
          if(sum%3==0) {
              System.out.println("YES");
          }
          else System.out.println("NO");
          return ;
      }
      if(remain>=0) {
          long div = remain / 4;
          long md  = remain % 4;
          long ans = (div*2) + (md==1?2: (md==2?6: (md==3?5:0)));
          if((ans+sum)%3==0)
              System.out.println("YES");
          else
              System.out.println("NO");
          return;
      }
    }	
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
      try
      {
        int t = input.nextInt();
        while(t-->0) {
          long n = input.nextLong();
          long a = input.nextLong();
          long b = input.nextLong();
          calc(n,a,b);
        }
      }
      catch(Exception e){
        return;
      }
    }
  }
  

Another Solution in C++

  #include <bits/stdc++.h>
  #include <limits.h>
  #define int long long
  using namespace std;

  void solve(){
      int k;
      int d0,d1;
      cin>>k>>d0>>d1;

      int s = d0+d1;
      int c = (2*s)%10 + (4*s)%10 + (8*s)%10 + (6*s)%10;

      int num_cycles = (k-3)/4;
      int tot = 0;

      if(k==2)
      {
          tot = s;
      }
      else
      {
          tot = s + s%10 + (c * num_cycles);
          int left_over = (k-3)-(num_cycles*4);
          int p=2;
          for(int i=1;i<=left_over;i++)
          {
              tot+=((p*s)%10);
              p = (p*2)%10;
          }
      }

      if((tot % 3) == 0)  
      	cout<<"YES\n";
      else 				  
      	cout<<"NO\n";	
  }
  int32_t main() {
      // your code goes here
      int t;
      cin>>t;
      while(t--)
      {
          solve();
      }
      return 0;
  }

Popular Posts

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Family Tree Project in Java

Creating basic tic tac toe android app using java