Sort by

recency

|

3 Discussions

|

  • + 0 comments

    Python 2 Code

    P = 10**9+7
    N = 100000
    for t in xrange(int(raw_input())):
        n = int(raw_input())
        ans = 0
        nn = min(n,N)
        K = 0
        for d in xrange(1,nn+1):
            K = n/d
            ans += K*(K-1)/2*d
            ans += (n%d)*K
        ans %=P
        if nn<n:
            dd = nn+1
            dn = 0
            dnr = 0
            rr = 0
            for i in xrange(1,N+1):
                dn = n/(i+1)
                if dn*(i+1)<n:
                    dn+=1
                if dn>n:
                    dn=n+1
                if dn-1>=dd:
                    ans += (dn-1+dd)*(dn-dd)/2*i
                if dn<dd:
                    dn=dd
                if dn<=n:
                    dnr = n-i*dn
                    if dnr>0:
                        rr = (dnr+i-1)/i
                        ans += (dnr+dnr-(rr-1)*i)*rr/2*i
        print (ans+n)%P
    
  • + 0 comments

    In last 5 testcases for n>10^5 I am getting timeout problem... Kindly help me...

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            for(int i=0; i<t; i++) {
               long n = sc.nextLong();
               long sum = n;
               
               for(long j=2; j<=n; j++) {
                   long k = (n/(j-1)*(n+n%(j-1)-(j-1))/2);  
                   sum = sum + k;
               }
               System.out.println(sum%1000000007);
            }
        }
    
  • + 0 comments

    Say for n = 6,

    is (1, 3) a valid AP or is it (1,3,5)

    which is valid?

    should we count both?

No more comments