zoukankan      html  css  js  c++  java
  • Codeforces Round #729 (Div. 2) C. Strange Function


    (题意:定义f(i)为不能整除i的最小整数,求displaystylesum_{i=1}^n f(i))


    思路:
    逐步筛掉因子,首先考虑f(i)=2的,肯定是奇数,也就是除了剩下的n/2个偶数,奇数部分已经可以不考虑了,而且贡献是2。
    然后剩下了Left = n/2个数,这些数已经是2的倍数了(剩下的那些偶数),再看这部分里面f(i)=3的,也就是不是3的倍数的,
    有多少个呢?有Left-n/6个(除6是因为这部分同时满足是2和3的倍数),贡献为3,剩下的Left更新为n/6。
    以此类推。


    举个例子:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ===> ans = 0
    一开始筛掉奇数,Left的部分为
    2 4 6 8 10 12 14 16 18 ===> ans += 9 * 2 => 18
    然后筛掉剩余部分不是是3的倍数的,只剩下:
    6 12 18: ===> ans += 6 * 3 => 36
    然后筛掉剩余部分不是4的倍数的(这个时候对应n/12,即是lcm(6,4)),只剩下:
    12 ===> ans += 2 * 4 => 44
    最后到筛吊剩余部分不是5的倍数的:
    (空) ===> ans += 1 * 5 => 49
    最后答案输出49


    view code
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include <queue>
    #include<sstream>
    #include <stack>
    #include <set>
    #include <bitset>
    #include<vector>
    #define FAST ios::sync_with_stdio(false)
    #define abs(a) ((a)>=0?(a):-(a))
    #define sz(x) ((int)(x).size())
    #define all(x) (x).begin(),(x).end()
    #define mem(a,b) memset(a,b,sizeof(a))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define rep(i,a,n) for(long long i=a;i<=n;++i)
    #define per(i,n,a) for(int i=n;i>=a;--i)
    #define endl '
    '
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> PII;
    const int maxn = 1e5+200;
    const int inf=0x3f3f3f3f;
    const double eps = 1e-7;
    const double pi=acos(-1.0);
    const int mod = 1e9+7;
    inline int lowbit(int x){return x&(-x);}
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
    inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
    inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
    inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
    inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
    int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
    
    ll lcm(ll x, ll y)
    {
        return (x*y)/(gcd(x,y));
    }
    
    int main()
    {
        int kase;
        cin>>kase;
        while(kase--)
        {
            ll n = read();
            ll a[] = {2,5,7};
            if(n<=2)
            {
                cout<<a[n-1]<<endl;
                continue;
            }
            ll Left = n;
            ll ans = 0;
            ll cur = 1;
            rep(i,2,55)
            {
                cur = lcm(cur,i);
                ll toLeft = n/cur;
                ans += (Left- toLeft)*i;
                ans %= mod;
                Left = toLeft;
                if(Left<=0) break;
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    
    

  • 相关阅读:
    完美解决SpringCloud无法上传大文件方法
    完美解决SpringBoot无法上传大文件方法
    完美解决SpringMVC无法上传大文件方法
    完美解决c#.net无法上传大文件方法
    完美解决csharp无法上传大文件方法
    完美解决c#无法上传大文件方法
    完美解决asp.net无法上传大文件方法
    完美解决java无法上传大文件方法
    P47 会话 tf.Session()
    P46 tensorflow的图
  • 原文地址:https://www.cnblogs.com/Bgwithcode/p/14970532.html
Copyright © 2011-2022 走看看