LA 3521 Joseph's Problem

题意:给你正整数n和k,然后计算从i到n k%i的和;

思路;如果n小于1000000,直接暴力计算,然后大于1000000的情况,然后在讨论n和k的大小,根据k%i的情况,你会发现规律,是多个等差数列,然后你把这些等差数列加上就是答案。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define ll long long
 5 using namespace std;
 6 
 7 ll n,k;
 8 ll Getsum(ll n)
 9 {
10     ll sum=0;
11     for(ll i=1; i<=n; i++)
12     {
13         sum+=k%i;
14     }
15     return sum;
16 }
17 
18 int main()
19 {
20     while(scanf("%lld%lld",&n,&k)!=EOF)
21     {
22         if(n<=1000000)
23         {
24             printf("%lld\n",Getsum(n));
25             continue;
26         }
27         ll ans=0;
28         ans+=max((ll)0,n-k)*k;
29         for(int i=2; i<=10000; i++)
30         {
31             if(i>k) break;
32             ll x1=k/(i-1)-k/i;
33             if(k/i>n)continue;
34             int s=k%(k/(i-1)),e=k%(k/i+1);
35             if(k/(i-1)>n)
36             { s=k%n;
37               x1=n-k/i;
38             }
39             ans+=(s+e)*x1/2;
40         }
41         if(k>10000)
42         {
43            ll m=k/10000;
44            ans+=Getsum(m);
45         }
46         printf("%lld\n",ans);
47     }
48     return 0;
49 }
View Code

 

posted @ 2015-01-21 14:30  null1019  阅读(252)  评论(0编辑  收藏  举报