Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one.
Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime.
You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers (a, b, c), for which the statement is false, and the numbers meet the condition l ≤ a < b < c ≤ r.
More specifically, you need to find three numbers (a, b, c), such that l ≤ a < b < c ≤ r, pairs (a, b) and (b, c) are coprime, and pair(a, c) is not coprime.
The single line contains two positive space-separated integers l, r (1 ≤ l ≤ r ≤ 1018; r - l ≤ 50).
Print three positive space-separated integers a, b, c — three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order.
If the counterexample does not exist, print the single number -1.
2 4
2 3 4
10 11
-1
900000000000000009 900000000000000029
900000000000000009 900000000000000010 900000000000000021
In the first sample pair (2, 4) is not coprime and pairs (2, 3) and (3, 4) are.
In the second sample you cannot form a group of three distinct integers, so the answer is -1.
In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three.
暴力;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x,y) cout<<"bug"<<x<<" "<<y<<endl; #define bug(x) cout<<"xxx "<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2e9+10,mod=6; const ll INF=1e18+10; int main() { ll l,r; scanf("%lld%lld",&l,&r); for(ll i=l;i<=r;i++) { for(ll j=i+1;j<=r;j++) { for(ll k=j+1;k<=r;k++) { if(i==j||i==k||j==k)continue; if(__gcd(i,j)==1&&__gcd(j,k)==1&&__gcd(i,k)!=1) return 0*printf("%lld %lld %lld ",i,j,k); } } } printf("-1 "); return 0; }
题意:第一个人需要cnt1个数,并且每个数都不被x整除,第二个人需要cnt2个数,并且每个数都不被y整除;
求最小的v,使得1-v,v个数可以使得两个都能拿到各自数目的数;
思路:二分v即可,主要在于check怎么写;
有点类似容斥的原理,先把两个都不能放的去掉,然后将不能放2的尽量放1 ,不能放1 的尽量放2;
然后把v剩余与cnt1,cnt2剩余比较;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x,y) cout<<"bug"<<x<<" "<<y<<endl; #define bug(x) cout<<"xxx "<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2e9+10,mod=6; const ll INF=1e18+10; ll check(ll x,ll a,ll b,ll cnt1,ll cnt2) { ll p=x/a; ll q=x/b; ll c=x/(a*b/__gcd(a,b)); p-=c; q-=c; cnt1-=min(cnt1,q); cnt2-=min(cnt2,p); return x-c-p-q>=cnt2+cnt1; } int main() { ll cnt1,cnt2,x,y; scanf("%lld%lld%lld%lld",&cnt1,&cnt2,&x,&y); ll l=1; ll r=1e10,ans; while(l<=r) { ll mid=(l+r)>>1; if(check(mid,x,y,cnt1,cnt2)) { r=mid-1; ans=mid; } else l=mid+1; } printf("%lld ",ans); return 0; }
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as nthe length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105).
Print n integers forming the permutation. If there are multiple answers, print any of them.
3 2
1 3 2
3 1
1 2 3
5 2
1 3 2 4 5
By |x| we denote the absolute value of number x.
题意:n个数1-n,随意排放,两两相差为x,使得x的不同的数目为K;
思路:构造思路题,1 n 2 n-1 构造k-1个,后面递增,递减差值都为1 即可;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x,y) cout<<"bug"<<x<<" "<<y<<endl; #define bug(x) cout<<"xxx "<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2e9+10,mod=6; const ll INF=1e18+10; int main() { int n,k; scanf("%d%d",&n,&k); int l=1,r=n,last=1; for(int i=1;i<=k;i++) { if(i&1)printf("%d ",l++),last=1; else printf("%d ",r--),last=0; } if(last)for(int i=l;i<=r;i++)printf("%d ",i); else for(int i=r;i>=l;i--)printf("%d ",i); return 0; }
思路:线段数区间并;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x,y) cout<<"bug"<<x<<" "<<y<<endl; #define bug(x) cout<<"xxx "<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2e9+10,mod=6; const ll INF=1e18+10; struct linetree { int sum[N<<2],lazy[N<<2]; void pushup(int pos) { sum[pos]=sum[pos<<1]&sum[pos<<1|1]; } void pushdown(int pos) { if(lazy[pos]) { sum[pos<<1]|=lazy[pos]; sum[pos<<1|1]|=lazy[pos]; lazy[pos<<1]|=lazy[pos]; lazy[pos<<1|1]|=lazy[pos]; lazy[pos]=0; } } void build(int l,int r,int pos) { lazy[pos]=0; if(l==r) { sum[pos]=0; return; } int mid=(l+r)>>1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); pushup(pos); } void update(int L,int R,int c,int l,int r,int pos) { if(L<=l&&r<=R) { sum[pos]|=c; lazy[pos]|=c; return; } pushdown(pos); int mid=(l+r)>>1; if(L<=mid) update(L,R,c,l,mid,pos<<1); if(R>mid) update(L,R,c,mid+1,r,pos<<1|1); pushup(pos); } int query(int L,int R,int l,int r,int pos) { if(L<=l&&r<=R) return sum[pos]; pushdown(pos); int mid=(l+r)>>1; int ans=((1<<30)-1); if(L<=mid) ans&=query(L,R,l,mid,pos<<1); if(R>mid) ans&=query(L,R,mid+1,r,pos<<1|1); return ans; } }; linetree tree; int l[N],r[N],x[N]; int main() { int n,q,ans=1; scanf("%d%d",&n,&q); for(int i=1;i<=q;i++) { scanf("%d%d%d",&l[i],&r[i],&x[i]); tree.update(l[i],r[i],x[i],1,n,1); if(tree.query(l[i],r[i],1,n,1)!=x[i]) ans=0; } for(int i=1;i<=q;i++) if(tree.query(l[i],r[i],1,n,1)!=x[i]) ans=0; if(!ans) printf("NO "); else { printf("YES "); for(int i=1;i<=n;i++) { int out=tree.query(i,i,1,n,1); printf("%d ",out); } } return 0; }
Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one.
Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime.
You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers (a, b, c), for which the statement is false, and the numbers meet the condition l ≤ a < b < c ≤ r.
More specifically, you need to find three numbers (a, b, c), such that l ≤ a < b < c ≤ r, pairs (a, b) and (b, c) are coprime, and pair(a, c) is not coprime.
The single line contains two positive space-separated integers l, r (1 ≤ l ≤ r ≤ 1018; r - l ≤ 50).
Print three positive space-separated integers a, b, c — three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order.
If the counterexample does not exist, print the single number -1.
2 4
2 3 4
10 11
-1
900000000000000009 900000000000000029
900000000000000009 900000000000000010 900000000000000021
In the first sample pair (2, 4) is not coprime and pairs (2, 3) and (3, 4) are.
In the second sample you cannot form a group of three distinct integers, so the answer is -1.
In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three.