原题链接:https://ac.nowcoder.com/acm/contest/392/A
Code(C++):
#include
#include
using namespace std;
const int N=1e5+10;
struct node{
int l,r;
}a[N];
bool operator <(const node &a, const node &b){
if(a.l==b.l) return a.r>b.r;
return a.l<b.l;
}
int main(){
int n,m; cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>a[i].l>>a[i].r;
sort(a+1,a+1+m);
if(a[1].l>1){
cout<<-1<<endl;
return 0;
}
int s=1,e=1,ans=1;
for(int i=1;i<=m;i++){
if(a[i].l<=s) e=max(e,a[i].r);
else{
ans++;
s=e+1;
if(a[i].l<=s) e=max(e,a[i].r);
else{
cout<<-1<<endl;
return 0;
}
}
if(e>=n) break;
}
if(e>=n) cout<<ans<<endl;
else cout<<-1<<endl;
return 0;
}
原题链接:https://ac.nowcoder.com/acm/contest/392/B
Code(C++):
#include
#define ll long long
using namespace std;
ll q_mul(ll a, ll b, ll mod){
ll ans=0,res=a;
while(b){
if(b&1) ans=(ans+res)%mod;
res=(res+res)%mod;
b>>=1;
}
return ans;
}
ll q_pow(ll a, ll b, ll mod){
ll ans=1,res=a;
while(b){
if(b&1) ans=q_mul(ans,res,mod);
res=q_mul(res,res,mod);
b>>=1;
}
return ans;
}
int main(){
int t; cin>>t;
while(t--){
ll a,b,mod; cin>>a>>b>>mod;
cout<<q_pow(a,b,mod)<<endl;
}
return 0;
}
Code(Java):
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- >0){
BigInteger a = in.nextBigInteger();
BigInteger b = in.nextBigInteger();
BigInteger mod = in.nextBigInteger();
BigInteger ans = a.modPow(b,mod);
System.out.println(ans);
}
}
}
Code(Python):
t=int(input())
for i in range(0,t):
s=input().split()
a=int(s[0])
b=int(s[1])
p=int(s[2])
res=1
while(b>0):
if(b&1):
res=res*a%p
a=a*a%p
b>>=1
res=res%p
print(res)
原题链接:https://ac.nowcoder.com/acm/contest/392/E
Code(C++):
#include
using namespace std;
const int N=1e5+10;
int n,k,a[N];
bool check(int x){
int sum=0;
for(int i=1;i<=n;i++)
sum+=(a[i]/x);
return sum>=k;
}
int main(){
cin>>n>>k;
int l=1,r=0;
for(int i=1;i<=n;i++){
cin>>a[i];
r=max(r,a[i]);
}
int ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
cout<<ans<<endl;
}
原题链接:https://ac.nowcoder.com/acm/contest/392/G
Code(C++):
#include
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
return b==0?a:gcd(b,a%b);
}
int main(){
ll a,b; cin>>a>>b;
cout<<gcd(a,b)<<endl;
return 0;
}
//__gcd(a,b);