1. 程式人生 > >LightOJ 1098 - A New Function (前n項的因子和 不包括本身和1)

LightOJ 1098 - A New Function (前n項的因子和 不包括本身和1)

blank func lld class space algorithm php 求和 scan

題意:http://www.lightoj.com/volume_showproblem.php?problem=1098

通過一個因子,求出與此因子相對應的其他因子,求和;

例如n=20的時候,當因子為2時,對應的 2(4),3(6),4(8),5(10),6(12),7(14),8(16),9(18),10(20)

當為3時,對應的為2(6),3(9),4(12),5(15),6(18)

此時要計算時要註意避免 2和3時之間有重復的情況。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include
<iostream> #include<queue> #include<map> #include<math.h> #include<string> #include<vector> using namespace std; #define INF 0x3f3f3f3f #define LL long long #define N 1000006 int main() { int T,t=1; scanf("%d",&T); while(T--) { LL e,f,n; scanf(
"%lld",&n); LL m=(int)sqrt(n); LL sum=0; for(LL i=2;i<=m;i++) { sum+=i;///本身這個因子滿足 e=i+1;///前面的已經統計 往後推 f=n/i;///一共有多少個 if(e>f) continue; sum+=(f-e+1)*i;///一共出現幾次這個因子 sum+=(f-e+1)*(e+f)/2;///因子對應的另一個因子 數列求和
} printf("Case %d: %lld\n",t++,sum); } return 0; }

LightOJ 1098 - A New Function (前n項的因子和 不包括本身和1)