湘潭 A simple problem













A simple problem

Accepted : 30   Submit : 303
Time Limit : 15000 MS   Memory Limit : 655360 KB





Problem Description


There is a simple problem. Given a number N. you are going to calculate
N%1+N%2+N%3+...+N%N.

Input

First line contains an integer T, there are T(1≤T≤50) cases. For each
case T. The length N(1≤N≤1012).

Output

Output case number first, then the answer.

Sample Input

1
5


Sample Output

Case 1: 4



Source

daizhenyang


 1 import java.io.*;
2 import java.awt.*;
3 import java.math.BigInteger;
4 import java.util.Scanner;
5
6 public class Main {
7
8 public static void main(String[] args) {
9 Scanner cin = new Scanner(System.in);
10 int T = cin.nextInt();
11 for(int t=1;t<=T;t++)
12 {
13 long n =cin.nextLong();
14 BigInteger m =solve(n);
15 System.out.println("Case "+t+": "+m);
16 }
17 }
18 static BigInteger solve(long n)
19 {
20 BigInteger sum = BigInteger.ZERO;
21 long k = (long)Math.sqrt(n*1.0);
22 if(k*k!=n) k++;
23 long y = n;
24 long a1,d=1,x;
25 while(y>=k+1)
26 {
27 a1=n%y;
28 x=(y-a1+d)/(d+1);
29 sum = sum.add(get(a1,x,d));
30 y=y-x;
31 d++;
32 }
33 for(int i=2;i<=y;i++)
34 {
35 sum=sum.add(BigInteger.valueOf(n%i));
36 }
37 return sum;
38 }
39 static BigInteger get(long a1,long x,long d)
40 {
41 BigInteger sum = BigInteger.ZERO;
42 BigInteger a = BigInteger.valueOf(a1);
43 BigInteger cur = BigInteger.valueOf(d);
44 BigInteger an = a.add(BigInteger.valueOf(x-1).multiply(cur));
45
46 an = an.add(a);
47 sum = an.multiply(BigInteger.valueOf(x));
48 sum = sum.divide(BigInteger.valueOf(2));
49 return sum;
50 }
51 }

湘潭 A simple problem,布布扣,bubuko.com

时间: 2024-08-12 00:56:48

湘潭 A simple problem的相关文章

湘潭-1203-A simple problem

地址:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1203 做法: 假如n等于10^12. 那么n%1...n%10^6暴力解.复杂度o(10^6) 对于任意的n%x=y; 得: x*t+y=n;(t>=1&&t<=sqrt(n)) 对于任意的t, 第一个x*t+y=n的x1为n/(t+1)+1; 最后一个x*t+y=n的x2为n/t; n%x1,n%(x1+1),n%(x1+2)......n%(x

NYOJ 707 A Simple Problem(结构体排序) 睡前一水~~

链接:click here 题意: A Simple Problem 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 You know, just as the title imply, this is a simple problem. In a contest, given the team-id, solved, penalty of all the teams, tell me the champion.If the numbers of solved pr

E - A Simple Problem with Integers

#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define N 100002 struct node { int l,r; long long lz,w; }q[4*N]; void pushup(int rt) { q[rt].w=q[rt*2].w+q[rt*2+1].w; } void pushdo

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

hdu 5349 MZL&#39;s simple problem

Problem Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : delete the minimum number (if the set is empty now,then ignore it) 3 : query the maximum numbe