codeforces #410 div2 题解(数学)

官方题解

http://codeforces.com/blog/entry/51652
这一次的题目基本都有很强的思维性。编写代码很简单.

C

给你一个序列 a1,a2,,an ,有一个操作 将 (ai,ai+1)(aiai+1,ai+ai+1) ,问最少的操作次数使得 gcd(a1,a2,,an)>1 .

观察一个简单的性质你就懂了.
d=gcd(ai,ai+1),d=gcd(aiai+1,ai+ai+1),d|ai+ai+1,daiai+1,d2ai,d2ai+1,d2d .
所以我们有 d=gcd(a1,a2,,an)|gcd(a1,a2,,2ai,2ai+1,,an)2gcd(a1,a2,,an) . 问题转换为当 d=1 时求最小的操作次数,将整个序列变为偶数.

Code

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include<cmath>
#include <cstring>
#include <map>
#define fi first
#define se second
#define INF 0x3f3f3f3f
using namespace std;
const int MOD = 1e9+7;
const int MAX_P = 2e4+10;
const int maxn =100000+10;
const int MAX_V = 5e5+10;
const int maxv = 1e6+10;
typedef long long LL;
typedef pair<int,int> Pair;

LL my_gcd(LL x,LL y){
   if(x==0 || y==0)return max(x,y);
   else return __gcd(x,y);
}


int a[maxn];
int main(int argc, char const *argv[]) {

   int n;
   cin>>n;
   int ans = 0;
   int k=0;

   for(int i=0 ; i<n ; ++i){

      int x;
      scanf("%d",&x );
      a[i] = x;
      if(x&1){
         k++;
      }else{
         ans+=(k+1)/2+k%2;
         k =0;
      }
   }
   ans+=(k+1)/2+k%2;
   int d = a[0];
   for(int i=1 ; i<n ; ++i){
      d = my_gcd(a[i],d);
   }
   if(n==1){
      std::cout << "NO" << '\n';
   }else{
      std::cout << "YES" << '\n';
      printf("%d\n",d==1? ans : 0 );
   }


   return 0;
}

D

首先我们将问题转化为找出一些数让其大于剩下的数,因此只要满住我们找出的数存在比他小的数就行了.
贪心的取

code

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include<cmath>
#include <cstring>
#include <map>
#define fi first
#define se second
#define INF 0x3f3f3f3f
using namespace std;
const int MOD = 1e9+7;
const int MAX_P = 2e4+10;
const int maxn =100000+10;
const int MAX_V = 5e5+10;
const int maxv = 1e6+10;
typedef long long LL;
typedef pair<int,int> Pair;

struct Node{
   int a,b,id;
   bool operator<(const Node & o)const{
      if(a == o.a)return b>o.b;
      else return a> o.a;
   }
};

Node a[maxn];
int main(int argc, char const *argv[]) {

   int n;
   cin>>n;
   for(int i=0 ;i<n ; ++i){
      scanf("%d", &a[i].a);
      a[i].id = i+1;
   }
   for(int i=0 ; i<n ; ++i){
      scanf("%d",&a[i].b );
   }

   sort(a,a+n);
   std::cout << n/2+1 << '\n';
   printf("%d ", a[0].id);
   for(int i = 2 ; i<n ; i+=2){
      if(a[i-1].b>=a[i].b){
         printf("%d ",a[i-1].id );
      }else printf("%d ",a[i].id );
   }
   if(n%2 == 0){
      std::cout << a[n-1].id << '\n';
   }
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值