0

please have a look at my code. My code has a very unfortunate error at

a[0]=0;

https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2302

    #include <iostream>
    #include <bits/stdc++.h>
    #include <vector>
    #include <string>
    using namespace std;

    #define ll long long int
    #define mod 1e9+7

    ll msum(ll a,ll b,ll m) { return (a%m+b%m)%m; }
    ll msub(ll a,ll b,ll m) { return (a%m-b%m)%m; }
    ll mpro(ll a,ll b,ll m) { return ((a%m)*(b%m))%m; }
    ll fxp(ll a,ll b,ll m) {
        if(b==0)
            return 1;
        if(b%2==0)
            return fxp(a*a,b/2,m);
        return fxp(a,b-1,m)*a;
    }
    void swap(ll &a,ll &b){ ll t=a; a=b; b=t;}

    int main()
    {
        ll t;
        ll n=12158598919;
        ll a[n+1];
        a[0]=0;
        a[1]=1;
        for(ll i=2;i<=n;i++)
            a[n]=i-1;
        for(ll i=2;i<=n;i++)
            for(ll j=2*i;j<=n;j+=i)
                a[j]-=a[i];
        cin>>t;
        while(t!=0)
        {

            ll i,sum=0,k;
            for( i=0;i<=n;i++)
            {
                sum+=a[i];
                if(sum>=t)
                    break;
            }
            t=t-(sum-a[i]);
            ll x=1;
            if(t==0)
                cout<<"0/1"<<endl;
            else
            for(ll j=1;j<i;i++)
            {
                if(__gcd(j,i)==1)
                {
                    x++;
                    if(x==t)
                        cout<<j<<"/"<<i<<endl;
                }
            }
        cin>>t;        
        }
     return 0;
    }
  • 1
    `ll a[n+1];` gives you a 90.6 GB large array. – mch May 06 '20 at 12:38
  • Welcome to SO! I notice a problem when you creating your "a" array, You can't create a stack allocated array with a length that is unknown at compile time (although it might be here). But the more pressing issue is that your array is very long and is probably not going to be allocated (more than 389.075 GB) – Henry Le Berre May 06 '20 at 12:38
  • This doesn't address the question, but that definition of `mod` (even though it's not used) is flawed. Consider `2*mod`, which will not give the result you're looking for. The macro should be rewritten as `#define mod (1e9+7)`. Even better, rewrite it as `const double mod = 1e9+7`. – Pete Becker May 06 '20 at 13:35
  • @PeteBecker Thank you for your suggestion – 201951003 ABHIJEET TAMRAKAR May 06 '20 at 20:00

0 Answers0