首页 > 其他分享 >Codeforces Round 895 (Div. 3)

Codeforces Round 895 (Div. 3)

时间:2023-10-01 20:44:17浏览次数:33  
标签:895 质数 Codeforces long Div sum define

A题简单的模拟计算,注意上取整的实现。

B题计算每个房间对应的每个最迟时间点,在这些时间点最取最小值,保证能安全通过所有房间。

D题拿到手就可以发现是贪心,但发现两部分会有冲突,也就是重复计算的部分。故提前找到两个数的lcm然后不计算lcm的倍数,为其他参与计算的数安排剩余数种的最大值和最小值,最后需要推导一一下等差数列求和

C题是构造题,需要给出一种构造使得两个非互质数相加在目标区间,我的思路有些单一化,确定了以2为gcd来进行构造,导致需要考虑细分的情况很多,写的也很琐碎,还用到了线性筛,以及深刻体会到了能用数组就不用map,被卡死了,难受(。

大致说一下我的思路:

  • 首先要找到4这个分界点,因为4能被分解2和2,这是可以给出的最小非质对了。
  • 对于大于4的左边界L,我们看右边界R是不是和L重合,若不重合,则区间内一定能找到大于4的偶数sum,将其分解成4,和sum-4,他们起码有gcd是2。
  • 如果重合,则这个数是偶数和刚刚一样处理即可。
  • 若是奇数,我们看它是不是质数,根据数论我们可以找到如果是质数的话一定是无解的。
  • 如果不是质数,则我们找到它的最小质因子f,分解成f和sum-f。
    以上是我的思路,为了保证时间效率,我提前用线性筛预处理范围内所有的质数和非质数的最小质因子,结果发现好像每次都处理也能来及,而且似乎时间还快了,我的是\(O(n)\),每次现场处里\(O(T*\sqrt{n})\)。
// Problem: C. Non-coprime Split
// Contest: Codeforces - Codeforces Round 895 (Div. 3)
// URL: https://codeforces.com/contest/1872/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
//# define int long long
#define ull unsigned long long
#define pii pair<int,int>
#define double long double
#define baoliu(x, y) cout << fixed << setprecision(y) << x
#define endl  "\n"

const int N = 1e7;
const int M = 1e7+ 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
int n, m;


bool st[N];
int mp[N];
void pre(){
	for(int i=2;i<=N;i++){
		if(st[i])continue;
		else{
			for(int j=2*i;j<=N;j+=i){
				st[j]=true;
				mp[j]=i;
			}
		}
	}
}
// void ans(){
	// for (int i = 2; i <= N; i ++ )
    // {
        // if (!st[i]) primes[cnt ++ ] = i;
        // for (int j = 0; primes[j] <= N / i; j ++ )
        // {
            // st[primes[j] * i] = true;
          // mp[primes[j] * i]= primes[j];
            // if (i % primes[j] == 0) {
//             	
            // break;
            // }
//            
        // }
    // }
// }
void solve(){
	int l,r;
	cin>>l>>r;
	if(r<=3){cout<<-1<<endl;return;}
	if(l<=4&&r>=4){cout<<2<<" "<<2<<endl;return ;}
	if(l!=r){
		if(l%2==0){cout<<4<<' '<<l-4<<endl;return ;}
		else {
			cout<<4<<' '<<l-3<<endl;return ;
		}
	}
	else {
		if(l%2==0){cout<<4<<' '<<l-4<<endl;return ;}
		else {
			if(st[l]==false){
				cout<<-1<<endl;return;
			}
			else {
				int f=mp[l];
				cout<<f<<" "<<l-f<<endl;
				return ;
			}
		}
	}
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);

    int t;
   cin>>t;
pre();
    while (t--) {
    
solve();
    }
    return 0;
}

这样思考时间花费太多,如果直接暴力枚举区间内的数,并且在线分解也是可以的。

标签:895,质数,Codeforces,long,Div,sum,define
From: https://www.cnblogs.com/mathiter/p/17739250.html

相关文章

  • Codeforces 1278D 题解
    题目大意题目大意给你\(n\)(\(1\leqslantn\leqslant5\cdot10^5\))条线段\([l_1,r_1],[l_2,r_2],\cdots,[l_n,r_n]\)(\(1\lel_i<r_i\le2n\))。保证每条线段的端点为整数,且\(\foralli,j\)(\(i\nej\)),不存在\(l_i=l_j\)或\(r_i=r_j\),不存......
  • Codeforces Round 811 (Div. 3)
    A.EveryoneLovestoSleep#include<bits/stdc++.h>usingnamespacestd;voidsolve(){intn,h,m,t;cin>>n>>h>>m;t=h*60+m;vector<int>a;for(inti=1,x,y;i<=n;i++)cin......
  • Codeforces 1702G2 题解
    题目大意给出一个大小为\(n\)的树,\(q\)次询问,每次给出一个大小为\(m\)的点集,判断是否有一条链覆盖这些点(这条链可以经过其他点)。\(n,\summ\leqslant2\cdot10^5\),\(q\leqslant10^5\)。提示提示1思考将$m$个点按深度排序。题解题解考虑将\(m\)个点按树......
  • CodeForces 1874B Jellyfish and Math
    洛谷传送门CF传送门看到这种操作乱七八糟不能直接算的题,可以考虑最短路。对于\(a,b,c,d,m\)按位考虑,发现相同的\((a,b,m)\)无论如何操作必然还是相同的。于是考虑对于每个可能的\((0/1,0/1,0/1)\),所有终态有\((c=0/1,d=0/1)\)或者不确定。这样我们对于一......
  • 「题解」Codeforces Round 895 (Div. 3)
    A.TwoVesselsProblem题目Sol&Code签到题#include<bits/stdc++.h>typedeflonglongll;intmin(inta,intb){returna<b?a:b;}intmax(inta,intb){returna>b?a:b;}intT,a,b,c;intmain(){scanf("%d"......
  • AT_abc254_h [ABC254Ex] Multiply or Divide by 2 题解
    打篇题解巩固一下。题意给你两个集合\(A\)和\(B\),对于任意\(A\)集合中的元素,我们可以进行\(2\)种操作:\(a_i\gets\left\lfloor\frac{a_i}{2}\right\rfloor\)或\(a_i\gets2\timesa_i\)。问最少要多少次才能使\(A\)和\(B\)中的元素相等。分析首先我们可以令\(a......
  • Codeforces Round 901 (Div. 2)
    CodeforcesRound901(Div.2)A-JellyfishandUndertale解题思路:卡在最后秒放。若\(x_i>(a-1)\):那么该\(x_i\)的贡献为\(a-1\)。否则,该\(x_i\)的贡献为\(x_i\)。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;typedefpair<int,in......
  • 901DIV2 (A~D)
    901DIV2A~DA:大于等于\(a-1\)的贡献都是a-1.voidsolve(){intans=0;inta,b,n;cin>>a>>b>>n;ans+=b;for(inti=1;i<=n;i++){intx;cin>>x;if(x>=a)x=a-1;ans+=x;}cout<<......
  • Educational Codeforces Round 155 (Rated for Div
    B.ChipsontheBoard题解:贪心显然我们可以把题意转化为:对于任意一个\((i,j)\),我们可以花费\(a_{i,j}\)的代价占据第\(i\)行和第\(j\)列,求占据所有格子的最小代价考虑两种情况:在每一行选一个格子在每一列选一个格子贪心选即可intn,a[N],b[N];voidsolve......
  • Codeforces Round 898 (Div. 4)
    由于题目补完才来写总结,导致前面的有的题目都需要重新再看一遍,只好当作复习了。但考虑到趁热打铁,先看H.H题:从小白视角来看乍一看是博弈论,仔细思考以后发现是图论。本题给的是基环树,意识到这一点很重要,这个条件是让本题不是很复杂的关键。n个点n条边且没有重边保证这个联通图中......