LA 3521 Joseph ' s problem

Source: Internet
Author: User

Test instructions: give you positive integers n and k, and then calculate the and from I to n k%i;

idea; if n is less than 1000000, the direct violence is calculated, then the case is greater than 1000000, and then in the discussion of the size of N and K, according to the k%i situation, you will find that the rule is multiple arithmetic progression, and then you add these arithmetic progression to the answer.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 #definell Long Long5 using namespacestd;6 7 ll N,k;8 ll Getsum (ll N)9 {Tenll sum=0; One      for(LL i=1; i<=n; i++) A     { -sum+=k%i; -     } the     returnsum; - } -  - intMain () + { -      while(SCANF ("%lld%lld", &n,&k)! =EOF) +     { A         if(n<=1000000) at         { -printf"%lld\n", Getsum (n)); -             Continue; -         } -ll ans=0; -Ans+=max (LL)0, n-k) *K; in          for(intI=2; i<=10000; i++) -         { to             if(i>k) Break; +ll x1=k/(i-1)-k/i; -             if(k/i>n)Continue; the             ints=k% (k/(i-1)), e=k% (k/i+1); *             if(k/(i1) >N) ${s=k%N;Panax Notoginsengx1=n-k/i; -             } theans+= (s+e) *x1/2; +         } A         if(k>10000) the         { +ll m=k/10000; -ans+=getsum (m); $         } $printf"%lld\n", ans); -     } -     return 0; the}
View Code

LA 3521 Joseph ' s problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.