FCF3P2 - Editorial




Author: Pritish Priyatosh Nayak

Tester: Niket Agarwal

Editorialist: Pritish Priyatosh Nayak




Prefix Sums , Observation.



Subham has come to a circular race track to practice for hurdle race.
The race track is divided into N sections.

In each of the sections, he has placed some hurdles. The i^{th} section has hurdles of height h_i.

He starts at section 1 and he runs in direction of increasing index (i.e from index i to index (i+1) . Note : After N^{th} index he comes to 1^{st} index ).

Each time he runs past a section having hurdles with height h_i , he burns h_i calories. He has decided to run until he has crossed K sections.

Tell him how many calories he burns after running through K sections modulo 10^9+7.


Hard Version Editorial Link: FCF3P1 - Editorial

As K is too large , we cannot simulate the process under time limit of 1 sec. But notice that if the race track is of length N, then the number of times Subham will circle the whole track is equal to floor(K/N). That means he will complete circling the race tracks floor(K/N) times. And the remaining sections to be covered is equal to K modulo N . And K modulo N is less than N , so that means for he will cover a prefix of the race track.

So for the answer we can simply multiply the sum of hurdle values of the entire track with the number of times he completely circles the track.
And add it with sum of values prefix having K modulo N elements.

See setter solution for more details.


Setter's/Editorialist's Solution
    using namespace std;
    int main()
       #ifdef Zoro
       freopen("/home/pritish/Competitive/in", "r", stdin);
       freopen("/home/pritish/Competitive/out", "w", stdout);
       int t;

          long long n,k;
          long long a[n];
          for (int i = 0; i < n; ++i)

          long long pref[n];
          const long long mod=1e9+7;
          for (int i = 1; i < n; ++i)
          long long full_turns=k/n,prefix=k%n;
          long long ans=(pref[n-1]*full_turns)%mod; 
       cerr<<"\n"<<(float)clock()/CLOCKS_PER_SEC*1000<<" ms"<<endl;
       return 0;
Tester's Solution
    import java.util.*;import java.io.*;import java.math.*;
    public class Main
        public static void process()throws IOException
            int n=ni();
            int k=ni();
            long pref[]=new long[n+1];
            for(int i=1;i<=n;i++)
            long ans=(((pref[n]*(k/n))%mod)+pref[k%n])%mod;
        static long mod=(long)1e9+7l;
        static AnotherReader sc;
        static PrintWriter out;
        public static void main(String[]args)throws IOException
            boolean oj = true;
            if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
            else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
            int t=1;
            while(t-->0) {process();}
        static void pn(Object o){out.println(o);}
        static void p(Object o){out.print(o);}
        static void pni(Object o){out.println(o);out.flush();}
        static int ni()throws IOException{return sc.nextInt();}
        static long nl()throws IOException{return sc.nextLong();}
        static double nd()throws IOException{return sc.nextDouble();}
        static String nln()throws IOException{return sc.nextLine();}
        static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
        static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
        static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
        static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
        static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
        static class AnotherReader{BufferedReader br; StringTokenizer st;
        AnotherReader()throws FileNotFoundException{
        br=new BufferedReader(new InputStreamReader(System.in));}
        AnotherReader(int a)throws FileNotFoundException{
        br = new BufferedReader(new FileReader("input.txt"));}
        String next()throws IOException{
        while (st == null || !st.hasMoreElements()) {try{
        st = new StringTokenizer(br.readLine());}
        catch (IOException  e){ e.printStackTrace(); }}
        return st.nextToken(); } int nextInt() throws IOException{
        return Integer.parseInt(next());}
        long nextLong() throws IOException
        {return Long.parseLong(next());}
        double nextDouble()throws IOException { return Double.parseDouble(next()); }
        String nextLine() throws IOException{ String str = ""; try{
        str = br.readLine();} catch (IOException e){
        e.printStackTrace();} return str;}}