Description
F (n) = (n% 1) + (n% 2) + (n% 3) + ... (n% n). Where% represents mod, which is the remainder.
For example f (6) = 6 1 + 6% 2 + 6% 3 + 6% 4 + 6% 5 + 6% 6 = 0 + 0 + 0 + 2 + 1 + 0 = 3.
Given n, calculate f (n).
Input
Enter 1 number n (2 <= n <= 10^12).
Output
Output f (n).
Sample Input6Sample Output3This is a problem on the 51NOD, because cannot submit, so get to our school OJ submitted. the topic n up to 10^12 is so large that it is not possible to go through it naturally. and then because n%i = N-[n/i]*iso sum (n%i) = SUM (n-[n/i]*i) = N*n-sum ([n/i]*i). Taking into account the part of the rounding , when I in a continuous period of time may be the value of the same. =>[n/i] = [n/j] + j = n/(n/i) Here the division is taken off the whole. then we can consider the grouping operation. because of the large data, C + + high precision is used. Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<string>#defineLL Long Longusing namespacestd;Const intUNIT =Ten;structbignum{intval[ -]; intLen; Bignum () {memset (Val,0,sizeof(Val)); Len=1; } bignumoperator=(ConstLL &a) {LL T, p=A; Len=0; while(P >=UNIT) {T= P-(p/unit) *UNIT; P= P/UNIT; Val[len++] =T; } Val[len++] =p; return* This; } bignumoperator+(ConstBignum &a)Const{bignum x=A; intL; L= A.len > Len?A.len:len; for(inti =0; i < L; ++i) {X.val[i]+=Val[i]; if(X.val[i] >=UNIT) {X.val[i+1]++; X.val[i]-=UNIT; } } if(X.val[l]! =0) X.len= L +1; ElseX.len=L; returnx; } bignumoperator-(ConstBignum &a)Const { BOOLFlag; Bignum x1, x2; if(* This>a) {x1= * This; X2=A; Flag=0; } Else{x1=A; X2= * This; Flag=1; } intJ, L =X1.len; for(inti =0; i < L; ++i) {if(X1.val[i] <X2.val[i]) {J= i+1; while(X1.val[j] = =0) J++; X1.val[j--]--; while(J >i) x1.val[j--] + = unit-1; X1.val[i]+ = unit-X2.val[i]; } ElseX1.val[i]-=X2.val[i]; } while(x1.val[x1.len-1] ==0&& X1.len >1) X1.len--; if(flag) X1.val[x1.len-1] =-x1.val[x1.len-1]; returnX1; } bignumoperator*(ConstBignum &a)Const{bignum x; intI, J, up; intx1, x2; for(i =0; i < Len; i++) { up=0; for(j =0; J < A.len; J + +) {x1= Val[i]*a.val[j] + x.val[i+j] +Up ; if(X1 >=UNIT) {X2= x1-x1/unit*UNIT; up= x1/UNIT; X.val[i+J] =x2; } Else{ up=0; X.val[i+J] =X1; } } if(Up! =0) X.val[i+J] =Up ; } X.len= i +J; while(x.val[x.len-1] ==0&& X.len >1) X.len--; returnx; } bignumoperator/(Const int&a)Const{bignum x; intDown =0; for(inti = len-1; I >=0; --i) {X.val[i]= (val[i]+down*unit)/A; down= Val[i] + down*unit-x.val[i]*A; } X.len=Len; while(x.val[x.len-1] ==0&& X.len >1) X.len--; returnx; } LLoperator%(ConstLL &a)Const{LL x=0; for(inti = len-1; I >=0; --i) x= ((X*unit)%a+val[i])%A; returnx; } BOOL operator> (ConstBignum &a)Const { intNow ; if(Len >A.len)return true; Else if(len = =A.len) { now= Len-1; while(Val[now] = = A.val[now] && now >=0) Now--; if(Now >=0&& Val[now] >A.val[now])return true; Else return false; } Else return false; }}ans, TMP, ttmp; LL N;voidWork () {ans=N; Ans= ans*ans; LL J; for(LL i =1; I <= N; i++) {J= n/(n/i); TMP= i+J; Ttmp= j-i+1; TMP= tmp*ttmp; TMP= tmp/2; Ttmp= n/i; TMP= tmp*ttmp; Ans= ans-tmp; I=J; }}voidoutput () { for(inti = ans.len-1; I >=0; --i) printf ("%d", Ans.val[i]); printf ("\ n");}intMain () {//freopen ("test.in", "R", stdin); while(SCANF ("%lld", &n)! =EOF) {work (); Output (); } return 0;}
View Code
ACM learning process-snnuoj1132 remainder sum (number theory)