zoukankan      html  css  js  c++  java
  • 牛客每日一题

    今天突然发现牛客上的每日一题也很宝藏~!!!决定刷一下~~~持续更新

    链接:https://ac.nowcoder.com/acm/problem/13221
    来源:牛客网
    数码(数论分块)

    题目描述

    给定两个整数 l 和 r ,对于所有满足1 ≤ l ≤ x ≤ r ≤ 10^9 的 x ,把 x 的所有约数全部写下来。对于每个写下来的数,只保留最高位的那个数码。求1~9每个数码出现的次数。
     
    倭瓜说一句,之前没学数论分块!有跟我一样的推:https://ac.nowcoder.com/acm/problem/blogs/13221
    题解里有人讲解,很详细~~
     
    #include<iostream>
    #include<algorithm>
    using namespace std;
      long long l,r;
    long long calculate(long long x,long long k)
    {
        long long h=0,ans=x/k;
        long long be=k*10,end=min(x,k*10+9);
        for(;be<=x;be*=10,end=end*10+9)
        {
            h=min(x,end);
            for(int i=be;i<=h;)
            {
                long long tag=x/i;
                long long lim=min(x/tag,h);
                ans+=tag*(lim-i+1);
                i=lim+1;
            }
        }
        return ans;
    }
    int main()
    {
        cin>>l>>r;
       for(int i=1;i<=9;i++)
       {
           cout<<calculate(r,i)-calculate(l-1,i)<<endl;
       }
        return 0;
    }
     
    链接:https://ac.nowcoder.com/acm/problem/14248
    来源:牛客网

    题目描述

    给定一棵n个点的树,问其中有多少条长度为偶数的路径。路径的长度为经过的边的条数。x到y与y到x被视为同一条路径。路径的起点与终点不能相同。
     
    emm。。。奇数层的点到奇数层为偶数路径。算出奇数点的个数和偶数点的个数,在奇数中任选两个点都为偶数即C(od,2),偶数中任选两个点都为偶数C(en,2);
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define maxn 110000
    #define ll long long
    long long n,cnt,head[maxn],dep[maxn],ans,ans1;
    struct edge{
        long long nx,to;
    }edge[maxn*2];
    void add(int u,int v)
    {
        edge[++cnt].nx=head[u];
        edge[cnt].to=v;
        head[u]=cnt;
    }
    void dfs(ll u,ll fa,ll depth)
    {
        if(depth%2==0) ans++;
        else ans1++;
        for(int i=head[u];i;i=edge[i].nx)
        {
            ll v=edge[i].to;
            if(v!=fa)
            {
                dfs(v,u,depth+1);
            }
        }
    }
    int main()
    {
        cin>>n;
        for(int i=1;i<n;++i)
        {
            ll x,y;
            cin>>x>>y;
            add(x,y),add(y,x);
        }
       dfs(1,0,0);
       cout<<ans*(ans-1)/2+ans1*(ans1-1)/2;
    }
     
    链接:https://ac.nowcoder.com/acm/problem/14731
    来源:牛客网

    题目描述

    求所有长度为n的01串中满足如下条件的二元组个数:
    设第i位和第j位分别位ai和aj(i<j),则ai=1,aj=0。
    答案对1e9+7取模。
    我真是太虚了!!组合数学还没看~~现在趁机学一波~,对于任意两个位置,假设其满足逆序对要求,则从中选出两个位置C(n,2),剩余各自安放,有2^n-2中选择;
    (虽然 题解这样说,但我还是有点懵),外加模运算真的很难受!!
     
    #include<iostream>
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    const ll mod=1e9+7;
    ll pow1(ll a,ll b)
    {
        ll ans=1;
        while(b)
        {
            if(b&1) ans=ans*a%mod;
            a=a*a%mod;
            b>>=1;
        }
        return ans;
    }
    int main()
    {
        ll n;
        cin>>n;
        if(n==1) cout<<0;
        else {
            ll ans1=pow1(2,n-2);
            ll k=((n%mod)*((n-1)%mod)/2);
            cout<<(ans1%mod)*(k%mod)%mod;
        }
        return 0;
    }

    链接:https://ac.nowcoder.com/acm/problem/23049
    来源:牛客网

    题目描述

    二月中旬虐狗节前夕,华华决定给月月准备一份礼物。为了搭建礼物的底座,华华需要若干根同样长的木棍。华华手头上有一些长度参差不齐的木棍,他想将每根都裁剪成若干段自己想要的长度,并丢掉多余的部分。因为华华的手很巧,所以他的裁剪过程不会有任何的失误。也就是说,对于一根长度为N的木棍,华华可以精准的将它们裁剪为若干段木棍,使它们的长度之和为N。
    华华不知道裁剪成多长比较好,所以干脆越长越好。不过由于华华有点强迫症,所以他希望长度为非负整数。保证所有木棍的原长也是非负整数。那么请问华华最终得到的每根木棍多长呢?
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define maxn 110000
    ll n,k,max1=0;
    ll L[maxn];
    bool check(ll mid)
    {
        ll ans=0;
        for(int i=1;i<=n;++i)
        {
            if(L[i]<mid) continue;
            ans+=L[i]/mid;
        }
        if(ans<k) return 0;
        else return 1;
    }
    int main()
    {
        cin>>n>>k;
        for(ll i=1;i<=n;++i) cin>>L[i],max1=max(max1,L[i]);
        ll l=1,r=max1,h=0;
        while(l<r)
        {
            ll mid=(l+r)/2+1;
            if(check(mid)) h=mid,l=mid+1;
            else r=mid-1;
        }
        if(check(h+1)) cout<<h+1;
        else cout<<h;
        return 0;
    }

     链接:https://ac.nowcoder.com/acm/problem/20273
    来源:牛客网

    题目描述

    windy有 N 条木板需要被粉刷。 每条木板被分为 M 个格子。 每个格子要被刷成红色或蓝色。 
    windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色。 每个格子最多只能被粉刷一次。 
    如果windy只能粉刷 T 次,他最多能正确粉刷多少格子? 
    一个格子如果未被粉刷或者被粉刷错颜色,就算错误粉刷。
    dp真是个神奇的东西;
     
     
    链接:https://ac.nowcoder.com/acm/problem/16655
    来源:牛客网

    题目描述

    在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧。在桥上有一些石子,青蛙很讨厌踩在这些石子上。由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数轴上的一串整点:0,1,……,L(其中L是桥的长度)。坐标为0的点表示桥的起点,坐标为L的点表示桥的终点。青蛙从桥的起点开始,不停的向终点方向跳跃。一次跳跃的距离是S到T之间的任意正整数(包括S,T)。当青蛙跳到或跳过坐标为L的点时,就算青蛙已经跳出了独木桥。

    题目给出独木桥的长度L,青蛙跳跃的距离范围S,T,桥上石子的位置。你的任务是确定青蛙要想过河,最少需要踩到的石子数。

    L过大,且M的数量较小;就需要离散化;
    #include<bits/stdc++.h>
    #include<algorithm>
    using namespace std;
    #define ll long long
    const ll maxn=2000000;
    int l,s,t,h,f[maxn],m,discre[maxn],stone[maxn];
    int dp[maxn];
    int main()
    {
        cin>>l>>s>>t>>m;
        for(int i=1;i<=m;i++)
        cin>>stone[i];
        sort(stone+1,stone+1+m);
        for(int i=1;i<=m;i++)
        {
            if(stone[i]-stone[i-1]>s*t) h+=(stone[i]-stone[i-1])%t+t*s;//离散化
            else h+=stone[i]-stone[i-1];
            discre[h]=1;
     
        }
        memset(dp,0x3f,sizeof(dp));
        dp[0]=0;
        for(int i=1;i<=h+t;i++)
        {
            for(int j=s;j<=t;j++)
                if(i>=j) dp[i]=min(dp[i],dp[i-j]+discre[i]);
        }
        int ans=0x3f;
        for(int i=h;i<=h+t;i++) ans=min(ans,dp[i]);
        cout<<ans;
    }
  • 相关阅读:
    apache https部署
    库位码排序优化
    mybatis + easy excel 导出百万级数据仅需要1g内存
    解决springboot打成jar包后, 无法获取(classpath)类路径下的自定义配置文件
    好用的 easyExcel 工具类
    数据结构与算法(相关名词)
    自动填充javabean属性,借助json序列化工具方便生成参数请求体
    用于避免bean对象连点方法调用报空指针异常,排查困难。
    基于FastJson封装的工具类
    itexpdf 工具类
  • 原文地址:https://www.cnblogs.com/Showend/p/12698892.html
Copyright © 2011-2022 走看看