[Codeforces]Codeforces Global Round 1

A - Parity

题意

给定一个$b$进制数,要求输出它在十进制下是奇数还是偶数。

分析

花了我略多的时间,首先题目中给的数字范围很大,不能直接转化为10进制。

分析性质,发现只有奇数乘奇数还是奇数,其他都是偶数。

对奇数进制和偶数进制分类讨论。

偶数进制看最低位的奇偶性,如果是奇数那么这个数就是奇数,不然是偶数。

奇数进制看每一位上奇数的个数,如果是奇数个奇数就是奇数,不然是偶数。

代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+1009;
 4 int read(){
 5     char c;int num,f=1;
 6     while(c=getchar(),!isdigit(c))if(c==‘-‘)f=-1;num=c-‘0‘;
 7     while(c=getchar(), isdigit(c))num=num*10+c-‘0‘;
 8     return f*num;
 9 }
10 int n,Base,v[N];
11 bool f1,f2;
12 int main()
13 {
14     Base=read();n=read();
15     for(int i=n-1;i>=0;i--)v[i]=read();
16     f1=Base&1;f2=0;
17     if(!f1){
18         f2=v[0]&1;
19         if(f2)printf("odd\n");
20         else printf("even\n");
21     }else{
22         for(int i=0;i<=n-1;i++)
23             if(v[i]&1)
24                 f2=!f2;
25         if(f2)printf("odd\n");
26         else printf("even\n");
27     }
28     return 0;
29 }

B - Tape

题意

给定一条线段,上面有$n$个点,你有$k$条线段(任意长度)可以使用,求覆盖所有点的最小线段长度和。

分析

一开始想用DP,秒得方程,但是复杂度是$O(n^2)$,想到用数据结构优化。

后来觉得B题不会这么难吧,仔细分析性质,每一个线段的两端肯定是两个点(显然)

然后每个点肯定仅由一条线段覆盖(显然)

那么问题就转化为了在题目中给的$n-1$个间隔中,选择$n-k$个间隔未被覆盖,其余的用线段覆盖。

然后排序一下就可以了。。

代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=5e5+1009;
 4 int read(){
 5     char c;int num,f=1;
 6     while(c=getchar(),!isdigit(c))if(c==‘-‘)f=-1;num=c-‘0‘;
 7     while(c=getchar(), isdigit(c))num=num*10+c-‘0‘;
 8     return f*num;
 9 }
10 int n,m,k,sum;
11 int a[N],dis[N];
12 int main()
13 {
14     n=read();m=read();k=read();
15     for(int i=1;i<=n;i++)a[i]=read();
16     for(int i=1;i<=n;i++)dis[i]=a[i]-a[i-1];
17     sort(dis+2,dis+n+1);
18     for(int i=2;i<n-k+2;i++)
19         sum+=dis[i];
20     printf("%d\n",sum+k);
21     return 0;
22 }

C - Meaningless Operations

题意

给定$q$个数,要求每个数$a$找到一个$b(1\le b< a)$使得$gcd(a$ $xor$ $b,a$ $and$ $b)$最大。

分析

我们分两类讨论。

第一类:$a$不是$2^k-1$时,这时候我们发现我们只要让$a$ $and$ $b=0$,$a$ $xor$ $b=2^k-1$就可以了。

重点在第二类:打表找规律,发现如果$a$是质数时,答案为$1$,否则答案为$a$的最大真因子。

我怕$a$很大又是质数的时候可能会超时,用的是Miller-Rabbin算法判断的质数。

代码

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const ll test[109]={2,3,5,7,11,61,24251,13,17,23};
 5 ll read(){
 6     char c;ll num,f=1;
 7     while(c=getchar(),!isdigit(c))if(c==‘-‘)f=-1;num=c-‘0‘;
 8     while(c=getchar(), isdigit(c))num=num*10+c-‘0‘;
 9     return f*num;
10 }
11 ll Pow(ll a,ll p,ll mod){
12     ll ans=1;
13     for(;p;p>>=1,a=1ll*a*a%mod)
14         if(p&1)ans=1ll*ans*a%mod;
15     return ans;
16 }
17 bool check(ll P){
18     if(P==1)return 0;
19     ll k=0,t=P-1;
20     while(!(k&1))k++,t>>=1;
21     for(int i=0;i<10;i++){
22         if(P==test[i])return 1;
23         ll a=Pow(test[i],t,P),nxt;
24         for(int j=1;j<=k;j++){
25             nxt=(1ll*a*a)%P;
26             if(nxt==1&&a!=1&&a!=P-1)return 0;
27             a=nxt;
28         }
29         if(a!=1)return 0;
30     }
31     return 1;
32 }
33 int ct(ll x){
34     ll t=1;int flag=0;
35     while(t<=x){
36         if((x&t)==0)flag=1;
37         t<<=1;
38     }
39     if(flag)return t;
40     else return -1;
41 }
42 int main()
43 {
44     int q=read();
45     for(int i=1;i<=q;i++){
46         ll a=read();
47         ll b=ct(a);
48         if(!(~b)){
49             if(check(a))printf("1\n");
50             else {
51                 for(ll i=2;i<a;i++){
52                     if(a%i==0){
53                         cout<<(a/i)<<endl;
54                         break;
55                     }
56                 }
57             }
58         }else cout<<(b-1)<<endl;
59     }
60     return 0;
61 }

原文地址:https://www.cnblogs.com/onglublog/p/10355788.html

时间: 2024-08-25 21:51:33

[Codeforces]Codeforces Global Round 1的相关文章

Codeforces Global Round 1 (A-E题解)

Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^(k-1)+a2*b^(k-2)+...+ak*b^0的奇偶性. 题解: 暴力求模2意义下的值就好了. 代码如下: #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+5; int

【手抖康复训练1 】Codeforces Global Round 6

[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思路手抖过不了样例,C题秒出思路手抖过不了样例*3 D题 手抖 过的了样例 ,调了1h,赛后发现变量名写错了,改一个字符就能AC... 题目等补完题一起放上来QAQ 原文地址:https://www.cnblogs.com/ttttttttrx/p/12110199.html

Codeforces Codeforces Round #484 (Div. 2) E. Billiard

Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/problem/E Description Consider a billiard table of rectangular size $n \times m$ with four pockets. Let's introduce a coordinate system with the origin at t

Codeforces Codeforces Round #484 (Div. 2) D. Shark

Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D Description For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and

Frets On Fire --- 2019 Codeforces Global Round 2 Problem D

原题:https://codeforces.com/contest/1119/problem/D 题意大概是一个n行1e18列的矩阵,其中每行第一个数为s[i],剩下的数每行依次以1的速度递增.就是说,矩阵元素 a[i][j] = s[i] + j .有q个询问,每个询问有两个参数l,r,求矩阵第l列到第r列(所有行)一共出现了几个不同的数. 这道题首先要先想到,两个参数 [l,r] 其实等价于一个参数 [0,r-l] ,也就是说矩阵第0~r-l行出现的数字的个数其实和第l-r行出现的个数是一样

【 Codeforces Global Round 1 B】Tape

[链接] 我是链接,点我呀:) [题意] x轴上有m个连续的点,从1标号到m. 其中有n个点是特殊点. 让你用k段区间将这n个点覆盖. 要求区间的总长度最小. [题解] 一开始假设我们需要n个胶带(即包含每一个点) 然后因为k<=n 所以可能胶带不够用. 那么就得一个胶带跨过两个点. 怎么选择最好呢? 可以把b[i]-b[i-1]-1处理出来排个序. (优先取较小的花费) 然后取前n-k个累加和sum. 因为每取一个就少用一段胶带. 然后sum+n就是答案了 [代码] import java.i

【Codeforces Global Round 1 C】Meaningless Operations

[链接] 我是链接,点我呀:) [题意] 给你一个a 让你从1..a-1的范围中选择一个b 使得gcd(a^b,a&b)的值最大 [题解] 显然如果a的二进制中有0的话. 那么我们就让选择的b的二进制中对应的位置为1 剩下全为0就好 这样a的二进制全都变成1之后就是答案了(gcd的右边是0). 但是如果a的二进制里面全是1的话. 就没办法这么构造了 这里有两种情况. ①.1的个数是偶数 那么就101010这样构造 另外一个数就是010101 答案就是010101转换成十进制 ②.1的个数是奇数

Codeforces Global Round 1

A. Parity 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 100010 5 int b, k, a[N]; 6 7 int main() 8 { 9 while (scanf("%d%d", &b, &k) != EOF) 10 { 11 int res = 0; 12 for (int i = 1; i <= k; ++i) scanf("%d&

【Codeforces Global Round 1 A】Parity

[链接] 我是链接,点我呀:) [题意] 给你一个k位数b进制的进制转换. 让你求出来转成10进制之后这个数字是奇数还是偶数 [题解] 模拟一下转换的过程,加乘的时候都记得对2取余就好 [代码] import java.io.*; import java.util.*; public class Main { static int N = (int)1e5; static InputReader in; static PrintWriter out; static int b,k; static