In case of fast I/O constraints, it should be mentioned in the problem what exactly we need to use and for all programming languages (at least a link to a page which would explain how we can perform fast IO). Because, it is hard to find during contest.
what’s the difference between this
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
print(1 + (n-1)*(n-2))
and this?
T = int(input())
for _ in range(T):
n = int(input())
print(1+(n-1)*(n-2))
I got 0.6 seconds for the Editorialist (Python) and 4.91 secs on the second one, (written by me)
can someone explain this pls?
Why does this give WA ?
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define fastio() ios_base::sync_with_stdio(false);cin.tie(0);
const int N = 1e6;
int main(){
fastio()
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int ans = 1 + ((n-1) * (n-2)) ;
cout<<ans<<endl;
}
return 0;
}
use “\n” instead of endl
The first one is a simple way to read input much faster in python.
In this problem most of the execution time ends up being the input and output because the actual computation is so trivial, so reading input faster makes a massive difference.
@kunal_1388 @rohit_00004
Both of you have n
declared as int
, which will cause an overflow when you multiply.
Thanks for the explanation!
try this in future for fast input output
/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. /
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int tc = Integer.parseInt(br.readLine());
while(tc–>0){
StringTokenizer st = new StringTokenizer(br.readLine());
long n = Long.parseLong(st.nextToken());
writer.write(((n-2)(n-1)+1) +“\n”);
}
writer.flush();
writer.close();
br.close();
}
}
try this in future for fast input output , I’m using this from starting thanks to blog of codeforces for fast input output
just search about it u will get slight_smile:
/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. /
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int tc = Integer.parseInt(br.readLine());
while(tc–>0){
StringTokenizer st = new StringTokenizer(br.readLine());
long n = Long.parseLong(st.nextToken());
writer.write(((n-2)(n-1)+1) +“\n”);
}
writer.flush();
writer.close();
br.close();
}
}
Thanks Will keep this in mind.
May I know how to see blogs of codeforces
Just simply search codeforces blog u will find top results from Google or u can check TOP blogs via top section of codeforces
Guys I am getting a WA for this ,can someone help me pointing out what’s wrong here?
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define vt vector<ll>
#define vvt vector<vt>
#define vvvt vector<vvt>
#define all(c) (c).begin(), (c).end()
#define rep(i, start, end) for(ll i=start; i<=end; i++)
#define sd(n) scanf("%lld",&n)
#define eps 1e-3
const ll mod=1e17+7;
const ll maxs=1e5+5;
#define dbg(x) cout<<"val of var is"<<x;
#define c(x) ll x;cin>>x
#define cc(x,y) ll x,y;cin>>x>>y
#define ccc(x,y,z) ll x,y,z; cin>>x>>y>>z
#define bitc __builtin_popcountll
#define fast cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false)
ll add(ll x,ll y ){ll res=x+y;return (res>=mod?res-mod:res);}
ll sub(ll x,ll y ){ll res=x-y;return (res<=0?res+mod:res);}
ll binpow(long long a, long long b) {a %= mod;long long res = 1;while (b > 0) {if (b & 1)res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}
ll mul(ll x,ll y){ll res=x*y;return(res>=mod?res%mod:res);}
ll inv(ll x){return binpow(x,mod-2);}
template<class T>void print(T &a){for(auto val: a)cout << val << " ";cout << "\n";}
template<class T>ll size(T &a){return a.size();}
template<class T>void read(vector<T> &a){for(ll i=0;i<size(a);i++)cin>>a[i];}
ll ceil2(ll a, ll b) { if (a == 0) return 0;return (a +b-1)/b ;}
inline ll maxim(ll a,ll b) {if(a>b) return a; else return b;}
inline ll minim(ll a,ll b) {if(a<b) return a; else return b;}
inline bool equals(double a, double b){ return fabs(a - b) < 1e-9; }
ll gcd(ll a, ll b) { return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a,ll b){return (b==0 or a==0) ? 0:mul(mul(a,b),inv(gcd(a,b)));}
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
ll allbit(ll n) { return (1LL << n) - 1; }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
vt primes(ll n){
bool prime[n+1];
vt p;
fill_n(prime,n+1,true);
for(int i=2;i*i<=n;i++){
if(prime[i]==true)
{
for(int j=i*i;j<=n;j+=i){
prime[j]=false;
}
}
}
for(int j=2;j<=n;j++){
if(prime[j])
p.pb(j);
}
return p;
}
vt prime_factors(ll n){
vt p;
for(ll i=2;i*i<=n;i++){
while(n%i==0){
p.pb(i);
n/=i;
}
}
if(n>1){
p.push_back(n);
}
return p;
}
int modInverse(int a, int m)
{
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
{
// q is quotient
int q = a / m;
int t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m, a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
ll fact[200010];
void compute_fact(ll modVal) {
fact[0]=1;
for(int i=1;i<=200004;i++) {
fact[i]=(fact[i-1]*i)%modVal;
}
}
ll ncr_mod(ll n,ll r,ll modVal) {
if(r==0||n==r) return 1;
if(n<r) return 0;
return (((fact[n]*modInverse(fact[r],modVal))%modVal)*modInverse(fact[n-r],modVal))%modVal;
}
#define lim 400009
void solve(){
int n;
cin>>n;
cout<<(n-1)*(n-2)+1<<"\n";
}
int main(){
fast;
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Yet another reminder
Consider the test input:
1
1000000000
Hey @iceknight1093 I am using the fastest I/O Operation for Java (according to the GFG Article) but still my code is getting TLE.
Please Help.
Link to my submission :-
https://www.codechef.com/viewsolution/63921482
Link to GFG Article that I reffered to :-
(I used method 4)
Hey @rahulahuja2901 ,
Your code is giving TLE is for the reason I/O. I have changed your code with the another fast I/O technique and it gives AC. here is the technique I used.