So I was working out a codeforces problem and found this out...
#include <iostream>
int main()
{
long long var = 1e17;
long long mod = 998244353;
std::cout << var % mod;
}
The output was:
470904831
which is actually not the right answer. The correct answer is:
470904832
Actually, in the problem, it was with loops using principles of modulo operator of multiplication and addition, but this is one of the examples where it failed.
If somebody can tell me what is going on, then it would be of great help.
Edit: I might have some misunderstanding, I compared the result with an online modulo calculator and it disagrees with the code... So, have a look at my code to the problem itself.
Link to the question:here.
The thing which I did in below code is as follows,
We notice that in f(ai,aj)=k can be written as follows,
suppose ai=12 and aj=34, k=1020 + 304=1324.
I call 1020 as "left shift" as the zero is appended from right and the number is shifted left.
Similarly 304 is called "right shift" and every number is added into the final result in both the forms and both the forms are occurring "n" times.
At the end, We just have to add those and return their modulo.
Till here, If there is any error in my working then please do tell.
Below is the implimentation,(Please don't be harsh on my ways of doing things, I am learning and if you can do it in few less lines then please don't judge)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//--<for debugging>
#define whatis(x) cout << #x << " is " << x<<" ";
#define whatisl(x) cout << #x << " is " << x<<"\n";
#define parr(array,end)for(ll loop=0;loop<end;loop++)cout<<array[loop]<<" ";cout<<"\n";
#define lline cout<<"\n";
#define errorl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);cout<<"\n";}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);cout<<"___";}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a <<" , ";
err(++it, args...);
}
//--</for debugging>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define testcase ll tt; cin >> tt; while(tt--)
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define vars ll h,i,j,k,d,l,p,q,r,x,y,a,b,c,v,var,n,m,z,s,ans,ind1,ind2,flag,limit1,limit2,limit3,mod;
#define vll vector<ll>
#define pll pair<ll,ll>
#define sll set<ll>
#define pint pair<int,int>
const ll INF=1e9+7;
const ll MOD=998244353;
int main()
{
boost vars
cin>>n;
ll arr[n];
for(i=0;i<n;i++)cin>>arr[i];
ans=0;
for(i=0;i<n;i++)
{
v=arr[i];
s=0;
p=10;
//left shift
while(v)
{
z=v%10;
v/=10;
s=(s+(p*z)%MOD)%MOD;
p=(p*100)%MOD;
//errorl(p);
}
error(s);
a=(s*n)%MOD;s=0;v=arr[i];p=1;
// right shift
while(v)
{
z=v%10;
v/=10;
s=(s+(p*z)%MOD)%MOD;
p=(p*100)%MOD;
}
b=(s*n)%MOD;
ans+=(a+b)%MOD;
errorl(s,a,b,ans);
}
cout<<ans;
}
The code gave wrong answer when Input =
5
1000000000 1000000000 1000000000 1000000000 1000000000
The correct answer is
265359409
My code gave answer =
2261848115
I highly appreciate your effort honestly, I didn't think that so many people will reply to this.