A simple problem(湘大邀请赛)
A simple problem |
||
Accepted : 61 | Submit : 418 | |
Time Limit : 15000 MS | Memory Limit : 655360 KB |
Problem DescriptionThere is a simple problem. Given a number N. you are going to calculate N%1+N%2+N%3+...+N%N. InputFirst line contains an integer T, there are T(1≤T≤50) cases. For each case T. The length N(1≤N≤1012). OutputOutput case number first, then the answer. Sample Input1 5 Sample OutputCase 1: 4 |
ps:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1203
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <climits> 5 #include <string> 6 #include <map> 7 #include <vector> 8 #include <set> 9 #include <list> 10 #include <cstdlib> 11 #include <cmath> 12 #include <algorithm> 13 #include <queue> 14 #include <stack> 15 #include <functional> 16 #include <complex> 17 #define mp make_pair 18 #define X first 19 #define Y second 20 #define MEMSET(a, b) memset(a, b, sizeof(a)) 21 using namespace std; 22 23 typedef unsigned int ui; 24 typedef long long ll; 25 typedef unsigned long long ull; 26 typedef pair<int, int> pii; 27 typedef vector<int> vi; 28 typedef vi::iterator vi_it; 29 typedef map<int, int> mii; 30 typedef priority_queue<int> pqi; 31 typedef priority_queue<int, vector<int>, greater<int> > rpqi; 32 typedef priority_queue<pii> pqp; 33 typedef priority_queue<pii, vector<pii>, greater<pii> > rpqp; 34 35 const int MAX_N = 100000 + 2; 36 const ll LL = 1000000000; 37 int a[MAX_N]; 38 39 struct bigNum 40 { 41 ll bit[3]; 42 43 bigNum() { 44 } 45 46 bigNum(const ll &b = 0) { 47 bit[0] = b % LL; 48 bit[1] = b / LL; 49 bit[2] = 0; 50 } 51 52 void format() { 53 bit[1] += bit[0] / LL; 54 bit[0] %= LL; 55 bit[2] += bit[1] / LL; 56 bit[1] %= LL; 57 } 58 59 bigNum operator * (const bigNum &bg) const { 60 bigNum tmp(0); 61 for (int i = 0; i < 3; ++i) { 62 for (int j = 0; j <= i; ++j) tmp.bit[i] += bit[j] * bg.bit[i - j]; 63 } 64 tmp.format(); 65 return tmp; 66 } 67 68 void operator = (const bigNum &bg) { 69 for (int i = 0; i < 3; ++i) bit[i] = bg.bit[i]; 70 } 71 72 void operator *= (const bigNum &bg) { 73 *this = *this * bg; 74 } 75 76 bigNum operator + (const bigNum &bg) const { 77 bigNum tmp(0); 78 for (int i = 0; i < 3; ++i) tmp.bit[i] = bit[i] + bg.bit[i]; 79 tmp.format(); 80 return tmp; 81 } 82 83 void operator += (const bigNum &bg) { 84 *this = *this + bg; 85 } 86 87 void half() { 88 if (bit[2] % 2) bit[1] += LL; 89 bit[2] /= 2; 90 if (bit[1] % 2) bit[0] += LL; 91 bit[1] /= 2; 92 bit[0] /= 2; 93 } 94 95 void print() { 96 bool flag = false; 97 if (bit[2]) printf("%I64d", bit[2]), flag = true; 98 if (flag) printf("%09I64d", bit[1]); 99 else if (bit[1]) printf("%I64d", bit[1]), flag = true; 100 if (flag) printf("%09I64d", bit[0]); 101 else printf("%I64d", bit[0]); 102 } 103 }; 104 105 int main(int argc, char *argv[]) 106 { 107 // freopen("D:\\in.txt", "r", stdin); 108 int t; 109 cin >> t; 110 for (int cas = 1; cas <= t; ++cas) { 111 ll n; 112 scanf("%I64d", &n); 113 bigNum bn(n), ans(0); 114 int k = (int)sqrt((double)n); 115 for (int i = 1; i <= k; ++i) { 116 bigNum tmp1(n / i - n / (i + 1)); 117 bigNum tmp2(n + n - i * (n / i + n / (i + 1) + 1)); 118 tmp1 *= tmp2; 119 tmp1.half(); 120 ans += tmp1; 121 } 122 ll tmp(0); 123 int lmt = (int)(n / (k + 1)); 124 for (int i = 1; i <= lmt; ++i) tmp += n % i; 125 bigNum tt(tmp); 126 ans += tt; 127 printf("Case %d: ", cas); 128 ans.print(); 129 puts(""); 130 } 131 return 0; 132 }
转载请注明出处:http://www.cnblogs.com/yuyixingkong/
自己命运的掌控着!