2020牛客寒假算法基礎集訓營2

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是整數,所以 根號(i
j) 也一定是整數,即 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;
}
發佈了11 篇原創文章 · 獲贊 0 · 訪問量 1740
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章