【Codeforces 476C】Dreamoon and Sums

【链接】 我是链接,点我呀:)
【题意】

让你求出所有x的和 其中 (x div b)是(x mod b)的倍数 且x mod b不等于0 且(x div b)除(x mod b)的值(假设为k),k∈[1..a]

【题解】

枚举k从1~a 这样k就固定了 n = x / b; m = x % b; n = m*k ····① x = b*n + m x = (b*k+1)*m 而m∈[1..b-1] 所以求和一下就是∑x = (b*k+1)*( b*(b-1)/2) 这里我们m属于1..b-1,显然由①式可知,k不同的时候,m从1到b-1变化的话, 得到的n也是不同的.(因此每一组(n,m)都不会相同) 所以如果我们枚举不同的k值,然后m从1到b-1变化不会得到重复的x值的,因为每一个x值 都唯一标识了一组(n,m),而(n,m)不会重复 枚举k加起来就好啦

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = 50000;
    static class Task{
        /*
         * n = x / b;
         * m = x % b;
         * n = m*k
         * x = b*n + m
         * x = (b*k+1)*m
         * m 1..b-1
         * ∑x = (b*k+1)*( b*(b-1)/2)
         * k 1..a
         */
    	long a,b;
    	long MOD = (int)(1e9+7),ans = 0;
        
        public void solve(InputReader in,PrintWriter out) {
        	a = in.nextInt();b = in.nextInt();
        	
        	for (long k = 1;k <= a;k++) {
        		long temp = (b*k+1)%MOD;
        		long temp1 = b*(b-1)/2%MOD;
        		temp = temp*temp1%MOD;
        		ans = (ans+temp)%MOD;
        	}
        	out.println(ans);
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}
原文地址:https://www.cnblogs.com/AWCXV/p/10559308.html