zoukankan      html  css  js  c++  java
  • 洛谷 题解 CF299A 【Ksusha and Array】

    本蒟蒻又双叒叕被爆踩辣!

    这就是道大水题

    首先,题目意思:

    给你n个数,要你找这些数字中找到一个能够被这些所有数字整除的数,若有多个,可任意输出其中一个,其实答案只有一个,因为在大于等于自己的数中能被自己整除的数只有它自己。

    一句话题意(虽然好像本来就是一句话

    要你找到一个数(a[x]),使: (gcd(a_1,a_2,a_3…a_n)) == (a[x])

    是不是只要推出了这一步就没有任何问题了,是吧。

    接下来,来证明一下(伪证

    窝们一样例为例

    10
    2 4 14 6 10 14 122 234 232 988
    

    上面这组数据的 (gcd(a_1,a_2,a_3…a_n)) 是不是为2.是不是(gcd(a_1,a_2,a_3…a_n)) (== a[1]) 成立。

    5
    122 244 366 2 488
    

    还是一样的,这样是不是很好理解,你要找的数肯定是当前这些数中最小的,而且要保证gcd(a_1,a_2,a_3…a_n)$ (== a[x])成立。

    代码:

    #include<bits/stdc++.h>
    
    using namespace std;
    
    const int dx[5] = {0, 1, -1, 0, 0};
    const int dy[5] = {0, 0, 0, 1, -1};
    
    //#define XRZ
    #define int long long
    #define maxn 100010
    #define maxm
    #define ll long long
    #define mian main
    #define inf 0x3f3f3f3f
    #define ls (x << 1)
    #define rs (x << 1 | 1)
    #define mid (l + r >> 1)
    #define debug(x) printf("now here is %d
    ", x);
    #define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout);
    #define Rep(x, a, b) for(int x = a; x <= b; ++ x)
    #define Dep(x, a, b) for(int x = a; x >= b; -- x)
    #define Next(x, u) for(int i = head[u]; i ; i = e[i].nxt)
    
    int n, a[maxn], b[maxn], c[maxn], ans;
    
    bool cmp(int x, int y) {return x > y;}//因为要用小的序号对于大的权值,所以从大到小排序。
    
    signed mian(){
    // file(a);
    scanf("%lld", &n);//读入
    Rep(i, 1, n){
    scanf("%lld%lld", &a[i], &b[i]);//输入每个角色的属性,好像a[],b[]并没有什么鸟用
    c[i] = a[i] - b[i];//这里是窝们求的(a[i] - b[i]),窝们只要维护它即可。
    ans += (n * b[i]) - a[i];//先加上定值。
    }
    sort(c + 1, c + n + 1, cmp);//排序。。。
    Rep(i, 1, n) ans += c[i] * i;//小的序号对于大的权值
    printf("%lld", ans);//输出
    return 0;
    }
    

    ps:请看懂再抄

  • 相关阅读:
    MS SqlServer学习笔记(索引)
    Angular动态注册组件(controller,service...)
    如何成功发布一个MSMQ的Windows服务
    主流Web服务器一览
    .NET 创建Windows服务,及服务的安装卸载
    SQL Server 查询时间段内数据
    委托和事件
    类中实现 Dispose And Finalize
    使用 ODBC .NET 提供程序和 Visual C# .NET 执行 SQL 参数化存储过程
    Windows Form 中快捷键设置
  • 原文地址:https://www.cnblogs.com/Flash-plus/p/12028197.html
Copyright © 2011-2022 走看看