Codeforces数学1600day3[数学CodeForces - 1213D2, CodeForces - 1165E 数论,CodeForces - 1165D 因子分解]

ps:day2太水了不写qwq
A - Equalizing by Division (hard version) CodeForces - 1213D2


题目大意:给你n个数和一个k,然后你可以执行·任意次操作每次操作把一个数/2向下取整。问最少执行多少次使得数组里有k个数相等


解题思路:
对于每次数除二的结果我们可以存下来然后枚举那个数出现了k次以上。sort一遍将结果相加


#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <deque>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define all(x) x.begin(),x.end()
#define next Next
#define f first
#define s second
using namespace std;
const int N = 2e5 + 10;
const double eps = 1e-9;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
inline LL read() {
   LL s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

int a[N];
vector<int> inf[N];

int main()
{
    int n = read(), k = read();
    _for(i,0,n) a[i] = read();
    
    _for(i,0,n)
    {
        int b = 0;
        while(a[i])
        {
            inf[a[i]].push_back(b);
            a[i] /= 2;
            b ++;
        }
    }
    
    int ans = INF;
    for_(i,N-1,0)
    {
        if(inf[i].size() >= k)
        {
            sort(inf[i].begin() ,inf[i].end());
            int res = 0;
            _for(j,0,k)
              res += inf[i][j];
              ans = min(ans,res);
        }
    }
    cout << ans << endl;
    return 0;
}


B - Two Arrays and Sum of Functions CodeForces - 1165E


题目大意:
求: ∑ i = 0 n − 1 ∑ j = i n − 1 ∑ k = i j a i ∗ b i \sum_{i=0}^{n-1}\sum_{j=i}^{n-1}\sum_{k=i}^{j}ai*bi i=0n1j=in1k=ijaibi其中·a数组是给定的,而b是可以任意排序的使得上面式子的结果最小


解题思路:假如我们暴力写就是:三个for循环时间复杂度高达 n 3 n^3 n3,那么我们看一下算一下每个i的贡献,就是计算一下每一个ai*bi算了多少次
举个例子: a 1 ∗ b 1 , a 2 ∗ b 2 , a 3 ∗ b 3 , a 4 ∗ b 4 , a 5 ∗ b 5 a1*b1,a2*b2,a3*b3,a4*b4,a5*b5 a1b1,a2b2,a3b3,a4b4,a5b5
在i=0时它们分别被算了5 4 3 2 1次
在i=1时它们分别被算了0 4 3 2 1次
在i=2时它们分别被算了0 0 3 2 1次
在i=3时它们分别被算了0 0 0 2 1次
在i=4时它们分别被算了0 0 0 0 1次
总的就是5 8 9 8 5次
r e s = a 1 ∗ b 1 ∗ 5 + a 2 ∗ b 2 ∗ 8 + a 3 ∗ b 3 ∗ 9 + a 4 ∗ b 4 ∗ 8 + a 5 ∗ b 5 ∗ 5 res = a1*b1*5 + a2*b2*8 + a3*b3*9 + a4*b4*8 + a5*b5*5 res=a1b15+a2b28+a3b39+a4b48+a5b55
因为a是不变的贡献也是不变的所以:先将ai乘以贡献sort一下再和b配对即可

#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <deque>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define all(x) x.begin(),x.end()
#define next Next
#define f first
#define s second
using namespace std;
const int N = 2e5 + 10, mod = 998244353;
const double eps = 1e-9;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
inline LL read() {
   LL s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

LL a[N];
LL b[N];
int main()
{
    int n = read();
    _for(i,1,n+1) a[i] = read();
    _for(i,1,n+1) b[i] = read();
    
    _for(i,1,n+1)
      a[i] = a[i] * i * (n - i + 1);
    sort(a+1,a+n+1);
    sort(b+1,b+1+n,greater<int>());
    LL res = 0;
    _for(i,1,n+1)
    {
        //这里不mod爆longlong
        res += ((b[i] % mod) * (a[i] % mod)) % mod;
        res %= mod;
    }
    
    cout << res << endl;
    return 0;
}


C - Almost All Divisors CodeForces - 1165D


一个简单的质因子分解:


#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <deque>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define all(x) x.begin(),x.end()
#define next Next
#define f first
#define s second
using namespace std;
const int N = 2e5 + 10, mod = 998244353;
const double eps = 1e-9;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
inline LL read() {
   LL s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
LL n;
LL a[330];

LL check(LL num)
{
    LL res = 0;
    for(LL i = 2; i * i <= num; ++ i)
        if(num % i == 0)
        {
            if(num / i != i) res += 2;
            else res ++;
        }
    // cout << res << endl;
    return res;
}

int main()
{
    IOS;
    int T;
    cin >> T;
    while(T --)
    {
      cin >> n;
      _for(i,0,n)
         cin >> a[i];
      sort(a,a+n);
      LL res = a[0] * a[n - 1];
      bool flag = true;
      LL l = 0,  r = n - 1;
      while(l < r)
      {
          LL now = a[l] * a[r];
          if(now != res)
          {
              flag = 0;
              break;
          }
          l ++;
          r --;
      }
      if(n & 1) 
      {
          if(a[l] * a[r] != res) flag = false;
      }
    //   cout << res << endl;
      if(!flag) cout <<"-1" << endl;
      else 
      {
          if(check(res) == n)  cout << res << endl;
          else cout << "-1" << endl;
      }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值