A,小贪心(注意,三个数相加会爆int)
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long a,b,c;
cin >> a >> b >> c;
long long x,y,z;
cin >> x >> y >> z;
long long ans = 0;
ans += min(a,y);
ans += min(b,z);
ans += min(c,x);
cout << ans << endl;
return 0;
}
B题:不需要去考虑你怎么对字符串进行变动
只需要统计6和1的个数,然后计算最多连续拼出多少个616即可。
注意:是连续(连续时可以节省6的使用)
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int yi = 0,liu = 0;
for(int i = 0; i < n; i++)
{
if(s[i] == '1')
yi++;
if(s[i] == '6')
liu++;
}
int ans = 0;
if(liu > yi)
{
ans = yi;
}
else
ans = liu - 1;
cout << ans << endl;
return 0;
}
D题:又是三角形,牛客第一天就是三角形…
n个点判断钝角三角形个数,我想到了camp里面杜教讲的向量。
使用向量来判断是不是钝角,有多少个钝角,就有多少个钝角三角形。(向量判断,三个点会有三个角,都要判断)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=acos(-1.0);
const double eps=1e-9;
int n;
struct node1
{
long long x,y;
}a[2010];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> a[i].x >> a[i].y;
}
long long ans = 0;
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
for(int k = j+1; k < n; k++)
{
long long x1,x2,y1,y2;
x1 = a[j].x - a[i].x;
x2 = a[k].x - a[i].x;
y1 = a[j].y - a[i].y;
y2 = a[k].y - a[i].y;
if(x1*x2+y1*y2 < 0 && x1*y2 != x2*y1)
ans++;
x1 = a[i].x - a[j].x;
x2 = a[k].x - a[j].x;
y1 = a[i].y - a[j].y;
y2 = a[k].y - a[j].y;
if(x1*x2+y1*y2 < 0 && x1*y2 != x2*y1)
ans++;
x1 = a[i].x - a[k].x;
x2 = a[j].x - a[k].x;
y1 = a[i].y - a[k].y;
y2 = a[j].y - a[k].y;
if(x1*x2+y1*y2 < 0 && x1*y2 != x2*y1)
ans++;
}
}
}
cout << ans << endl;
return 0;
}
E题,听其他大佬讲解。
将给的式子左右平方,便有 i+j+2根号(ij)=k
已知k是整数,所以 根号(ij) 也一定是整数,即 i*j 为完全平方数
只需要遍历n内所有的完全平方数,再对他们分解因数判断因数对数即可(注意1——1只有一对,单独处理)
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
long long ans = 0;
for(int i = 1; i*i <= n; i++)
{
int j = i*i;//完全平方数
for(int k = 1; k < i; k++)
{
if(j % k == 0)
ans += 2;
}
ans++;
}
cout << ans << endl;
return 0;
}
F题
两人都只想要拉大彼此的分数差,所有只需要这样想:
牛牛拿走一个物品后,相当于牛牛自己+a,同时牛可乐-b
牛可乐拿走物品同理
所有只需要对物品们按a+b排序,轮回拿最大的那个即可。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 50;
struct node1
{
int id;
long long a,b;
long long cha;
}node[maxn];
bool cmp(node1 a, node1 b)
{
return a.cha > b.cha;
}
int x[maxn];
int ansx = 0;
int y[maxn];
int ansy = 0;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> node[i].a;
node[i].id = i+1;
}
for(int i = 0; i < n; i++)
{
cin >> node[i].b;
node[i].cha = node[i].a + node[i].b;
}
sort(node,node+n,cmp);
for(int i = 0; i < n; i++)
{
if(i % 2 == 0)
{
x[ansx] = node[i].id;
ansx++;
}
else
{
y[ansy] = node[i].id;
ansy++;
}
}
for(int i = 0; i < ansx; i++)
{
if(i != ansx - 1)
cout << x[i] << " ";
else
cout << x[i] << endl;
}
for(int i = 0; i < ansy; i++)
{
if(i != ansy - 1)
cout << y[i] << " ";
else
cout << y[i] << endl;
}
return 0;
}
G题:快速幂取模
因为在计算的时候数据的最大值会很大很大,所以我选用了两的大素数来进行取模判断,hash定理
只有两个素数都满足,才符合。
#include <iostream>
#include <algorithm>
using namespace std;
const int mod1 = 1e9 + 7;
const int mod2 = 998344357;
long long quickPower(long long x, long long y,int mod)//快速幂加取模
{
long long result = 1; // 定义答案
while (y > 0) // 当指数大于 0 时进行幂运算
{
if (y & 1) // y 和 1 做与运算,相当于对 2 求模
{
result = (result * x) % mod;// 如果 y 为奇数,则结果只乘一个 x
}
x = x * x % mod; // x 乘二次方,下次使用
y = y >> 1 % mod; // y 右移一位,相当于除以 2
}
return result % mod; // 返回结果
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
long long a,b,c,d,e,f,g;
cin >> a >> b >> c >> d >> e >> f >> g;
if((quickPower(a,d,mod1) + quickPower(b,e,mod1) + quickPower(c,f,mod1)) % mod1== g % mod1 && (quickPower(a,d,mod2) + quickPower(b,e,mod2) + quickPower(c,f,mod2) ) % mod2== g % mod2)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
}
return 0;
}