2016华中农业大学预赛 B 数学

Problem B: Handing Out Candies

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 272  Solved: 20
[Submit][Status][Web Board]

Description

    After the 40th ACM-ICPC, Diao Yang is thinking about finding a girlfriend because he feels very lonely when doing ACM all the time. But because of his philandering, he finally decided to find N girlfriends. To achieve his goal, he wanted to find one girlfriend every day for N days continue. That is to say, at the ith day, he will have i girlfriends exactly.

    In order to make his N girlfriends happy, he decided to buy M candies everyday for N days continue. Every day all of his girlfriends can get candies, and he will give each of them the same amount of candies and the amount will be as much as possible. Then if there are some candies left, he will eat them by himself.

    Now the problem is, Diao Yang want to know how many candies he can eat total by himself after N days continue.

Input

    The first line contains an integer T, indicating the total number of test cases. Each test case is a line with two integers N 15N"> and M ( 151鈮?/m:t>N&lolt;231"> , 150鈮?/m:t>M&lolt;231"> ).

Output

    For each test case, output the answer in one line.   n,m在int范围内

Sample Input

2
5 7
6 4

Sample Output

7
9

题意:t组数据 输入n,m 求 m%i(1<=i<=n)的n项和

题解:思想就是 分段成部分等差数列 用求和公式求解

例如 n=60 m=100
m%(m/2)
m%51=49
m%52=48
....
m%60=40; 51~60 为等差为1的数列
....................................
m%(m/3)
m%34=32;
m%35=30;
...
m%49=2; 34~49 为等差为2的数列
....................................
知道m%(m/sqrt(m))

gou代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <string>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <map>
 9 #include <set>
10 #include <stack>
11 #include <sstream>
12 #include <vector>
13 using namespace std;
14 int main()
15 {
16     long long t,n,m,zhong,qi,di,chang;
17     long long ma;
18     scanf("%lld",&t);
19      while(t--)
20      {
21         scanf("%lld %lld",&n,&m);
22         ma=0;
23         if(m==1){
24             printf("%lld
",n-1);
25             continue;
26         }
27         if(m==0)
28         {
29             printf("0
");
30             continue;
31         }
32         if(m<=n){
33             ma+=(n-m)*m;
34             n=m-1;
35         }
36         zhong=sqrt(m);
37         for(int i=m/n;i<zhong;i++)
38         {
39             qi=m%n;
40             chang=n-(m/(i+1));
41             di=qi+(chang-1)*i;
42             ma+=(qi+di)*chang/2;
43             n=n-chang;
44         }
45         for(int i=n;i>1;i--)
46             ma+=m%i;
47         printf("%lld
",ma);
48      }
49     return 0;
50 }
原文地址:https://www.cnblogs.com/hsd-/p/5495443.html