洛谷 - P1403 约数研究(数学)

    技术2022-07-11  94

    题目传送 题意: 思路: 其实就是一个公式,1到n的所有约数的个数为:f(i) = n/i 这个函数i从1到n的函数值之和。

    证明: 1到n中: 拥有1的因子的个数为n 拥有2的因子的个数为n/2 只有偶数才有2,依次类推 拥有3的因子的个数为n/3 … 拥有n的因子的个数为n/n

    证毕 AC代码

    #include <bits/stdc++.h> inline long long read(){char c = getchar();long long x = 0,s = 1; while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();} while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();} return x*s;} using namespace std; #define NewNode (TreeNode *)malloc(sizeof(TreeNode)) #define Mem(a,b) memset(a,b,sizeof(a)) #define lowbit(x) (x)&(-x) const int N = 1e5 + 10; const long long INFINF = 0x7f7f7f7f7f7f7f; const int INF = 0x3f3f3f3f; const double EPS = 1e-7; const double EEE = exp(1); const int mod = 1e9+7; const double II = acos(-1); const double PP = (II*1.0)/(180.00); typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> piil; signed main() { std::ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ll n,sum = 0; n = read(); for(int i = 1;i <= n;i++) sum += (n/i); printf("%lld\n",sum); }
    Processed: 0.011, SQL: 9