A - Maximum GCD
0 与 7的 最大公约数是 7,范围错误的话会出错
#include<bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
const int N=1e7+10;
const int MAXN=20010;
const int INF=0x3f3f3f3f;
const double eps=0.0000001;
const ll mod=1e9+7;
int n,m,x,y,t;
string str;
int a[N];
int main()
{
//cout << "Hello world!" << endl;
cin>>n; getchar();
while(n--)
{
getline(cin,str);
int ans=0,cnt=0;
memset(a,0,sizeof(a));
for(int i=0;i<str.size();i++)
{
if(str[i]==' '&&ans!=0)
{
a[cnt++]=ans;
ans=0;
continue;
}
ans*=10;
ans+=str[i]-'0';
}
if(ans!=0)//注意最后一个数的处理
a[cnt++]=ans;
int p=0;
for(int i=0;i<cnt;i++)
{
for(int j=i+1;j<cnt;j++)
p=max(p,__gcd(a[i],a[j]));
}
cout<<p<<endl;
}
}
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<functional>
#include<set>
#include<map>
#include<stack>
#include<iterator>
#include<queue>
#include<vector>
#include<string>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
const int N=1e7+10;
const int MAXN=20010;
const int INF=0x3f3f3f3f;
const double eps=0.0000001;
const ll mod=1e9+7;
ll n,m,x,y,t;
string str;
int a[N];
ll pow_mod(ll a, ll b, ll p){//a的b次方求余p
ll ret = 1;
while(b){
if(b & 1) ret = (ret * a) % p;
a = (a * a) % p;
b >>= 1;
}
return ret;
}
ll Fermat(ll a, ll p){//费马求a关于b的逆元
return pow_mod(a, p-2, p);
}
int main()
{
//cout << "Hello world!" << endl;
cin>>n; getchar();
while(n--)
{
cin>>x>>y;
cout<<x*Fermat(y,9973)%9973<<endl;
}
}
C - 青蛙的约会
题解:拓展欧几里和 找到 题意与算法的关系 然后套板子就好了
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<functional>
#include<set>
#include<map>
#include<stack>
#include<iterator>
#include<queue>
#include<vector>
#include<string>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
const int N=1e7+10;
const int MAXN=20010;
const int INF=0x3f3f3f3f;
const double eps=0.0000001;
const ll mod=1e9+7;
string str;
void ex_gcd(ll a, ll b, ll &x, ll &y, ll &d){
if (!b) {d = a, x = 1, y = 0;}
else{
ex_gcd(b, a % b, y, x, d);
y -= x * (a / b);
}
}
ll inv(ll t, ll p){//如果不存在,返回-1
ll x,y,d;
ex_gcd(t, p, x, y, d);
return x;
}
///x=x0+K*b/gcd(a, b)
///y=y0+K*a/gcd(a, b)
int main(){
ll l, n,m,x,y,d;
scanf("%lld%lld%lld%lld%lld", &x,&y,&m,&n,&l);
ll a,b,c;
c=x-y;
b=l;
a=n-m;
if(c%(__gcd(a,b)))
{
cout<<"Impossible"<<endl;
return 0;
}
ll t=inv(a,b);
ll ans=b/__gcd(a, b);
cout<<((t*c/__gcd(a,b))%ans+ans)%ans<<endl;
}
Alyona and a Narrow Fridge
题解:二分
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<functional>
#include<set>
#include<map>
#include<stack>
#include<iterator>
#include<queue>
#include<vector>
#include<string>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
const int N=1e7+10;
const int MAXN=20010;
const int INF=0x3f3f3f3f;
const double eps=0.0000001;
const ll mod=1e9+7;
string str;
int a[N];
int n,m;
int slove(int x)
{
vector<int>p;
for(int i=0;i<=x;i++)
{
p.push_back(a[i]);
}
sort(p.begin(),p.end());
reverse(p.begin(),p.end());
ll cnt=0;
for(int i=0;i<p.size();i+=2) cnt+=p[i];
//cout<<cnt<<" "<<x<<endl;
return cnt<=m;
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int l=0,r=n-1,ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(slove(mid))
{
l=mid+1;
ans=mid;
}
else
r=mid-1;
}
cout<<ans+1<<endl;
}
筱玛爱地理
题解:
1.模的除法运算用逆元来求
2.然后就是注意下排序的方法,以及不要忘记mod取余。
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<functional>
#include<set>
#include<map>
#include<stack>
#include<iterator>
#include<queue>
#include<vector>
#include<string>
#define mp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, int> pli;
typedef pair<LL, LL> pll;
typedef long double ld;
const int N=1e6+10;
const int MAXN=20010;
const int INF=0x3f3f3f3f;
const double eps=0.0000001;
const LL mod=1e9+7;
string str;
pll a[N];
LL n,m;
LL pow_mod(LL a, LL b, LL p){//a的b次方求余p
LL ret = 1;
while(b){
if(b & 1) ret = (ret * a) % p;
a = (a * a) % p;
b >>= 1;
}
return ret;
}
LL Fermat(LL a, LL p){//费马求a关于b的逆元
return pow_mod(a, p-2, p);
}
bool cmp(pll a,pll b)
{
return a.second*b.first>b.second*a.first;
}
int main()
{
cin>>n;
LL x,y;
for(LL i=0;i<n;i++)
{
cin>>x>>y;
a[i]={x,y};
}
sort(a,a+n,cmp);
for(LL i=0;i<n;i++)
cout<<Fermat(a[i].first,mod)*a[i].second%mod<<endl;
}