How to solve ? Please Help

Problem Link
(Contest Page | CodeChef)
My Code Given TLE

#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);cin.tie(0);
#define lli long long int
#define vi vector<lli>
#define pb push_back
#include<string>
#define mp map<string,lli>
#define mpp map<lli,lli>
#define SS set<lli>
#define pq priority_queue<lli>
#define f first
#define S second
#define YES cout<<"YES\n";
#define NO cout<<"NO\n";
#define mod 1000000007
#define test lli t;cin>>t;while(t--)
using namespace std;
int main()
{
    test
    {
        lli n,sum=0;
        cin>>n;
        lli term=11+(n-1)*6;
        sum=(n*(11+term))/2;
        cout<<sum%mod<<endl;
    }
}

Make the following two changes

  • Add this line before reading input: ios_base::sync_with_stdio(false); cin.tie(NULL);
  • Change endl to '\n': cout << (sum % mod) << '\n';

The first one is for reading the input fast, while the second one is for flushing the output faster using '\n' character.

Here’s the accepted submission: Solution: 51262232 | CodeChef

2 Likes

Try using fast io (i.e. actually call fast instead of just defining the macro), and using \n instead of endl.

2 Likes