AtCoder Beginner Contest 169

AtCoder Beginner Contest 169

A - Multiplication 1

題目描述

輸出A * B

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <deque>
using namespace std;
typedef unsigned long long ll;
//__builtin_popcount(n);
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define RI register int
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int SZ = 2e5 + 10;
int n;

int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	printf("%d\n",a * b);
	return 0;
} 

B - Multiplication 2

題目描述

求A1 * A2 * … * An
2 ≤ N ≤ 10^5
0 ≤ Ai ≤ 10^18
如果超過1e18 輸出 -1

Solution

Java大數

代碼

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n;
        n = cin.nextInt();
        BigInteger x,ans;
        ans = new BigInteger("1");
        int flag = 0;
        for(int i = 1;i <= n;i ++)
        {
            x = cin.nextBigInteger();
            if(x.compareTo(BigInteger.valueOf(0)) == 0)
            {
                ans = BigInteger.valueOf(0);
                flag = 1;
            }
            if(flag == 0) ans = ans.multiply(x);
            if(ans.compareTo(new BigInteger("1000000000000000000")) > 0) flag = 1;
        }
        if(ans.compareTo(new BigInteger("1000000000000000000")) > 0)
        {
            System.out.println("-1");
        }
        else System.out.println(ans);
    }
}

C - Multiplication 3

題目描述

A * B
0 ≤ A ≤ 10^15
0 ≤ B < 10
A是正整數
B是有2位小數的浮點數

Solution

long double 存
結果下取整

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <deque>
using namespace std;
typedef long long ll;
//__builtin_popcount(n);
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define RI register int
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int SZ = 2e5 + 10;
int n;
long double a,b;
int main()
{
	cin >> a >> b;
	cout << (ll)(a * b) << endl;
	return 0;
} 

D - Div Game

題目描述

給定一個數n,每次把它除以一個質數的冪。最多可以除以多少個不同的數。

Solution

處理出素因子的個數,然後從小的冪次開始選,統計答案即可。

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <deque>
using namespace std;
typedef long long ll;
//__builtin_popcount(n);
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define RI register int
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int SZ = 2e5 + 10;
ll n;

vector<int> prime_factor(ll n)
{
	vector<int> res;
	for(ll i = 2;i * i <= n;i ++)
	{
		int temp = 0;
		while(n % i == 0)
		{
			temp ++;
			n /= i;
		}
		if(temp != 0) res.push_back(temp); 
	}
	if(n != 1) res.push_back(1);
	return res;
} 

int main()
{
	scanf("%lld",&n);
	vector<int> prime =  prime_factor(n);
	ll ans = 0;
	for(auto x: prime)
	{
		int now = 1;
		while(x - now >= 0) 
		{
			x -= now;
			now ++;
			ans ++;
		}	
	}
	printf("%lld\n",ans);
	return 0;
} 

E - Count Median

題目描述

給你n個區間,每個區間是在Ai到Bi之間,從每個區間中取出一位數,並求出這些數的中位數。問中位數有多少種取值可能。n爲偶數時,中位數是中間兩個數的平均數,可能有1/2的出現。

Solution

結論:最小和最大的中位數之間的所有可能的中位數都是一定可以取到的。

偶數和奇數分開處理即可。

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <deque>
using namespace std;
typedef long long ll;
//__builtin_popcount(n);
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define RI register int
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int SZ = 2e5 + 10;
int n;
ll A[SZ],B[SZ];

int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i ++) scanf("%lld%lld",&A[i],&B[i]);
	sort(A + 1,A + n + 1);
	sort(B + 1,B + n + 1);
	if(n % 2 == 1) printf("%lld\n",B[n / 2 + 1] - A[n / 2 + 1] + 1);
	else printf("%lld\n",B[n / 2] + B[n / 2 + 1] - A[n / 2] - A[n / 2 + 1] + 1);
	return 0;
} 

F - Knapsack for All Subsets

題目描述

求序列 A 的所有連續子段的 滿足元素的和等於 S 的子序列數量 的和。

Solution

揹包問題變式

令dp[i][j]爲前i個數中區間大小不超過i的有至少一個子集的元素和爲j的集合個數(多個不同子集和爲j則記錄多次)
dp[i][j] = dp[i - 1][j](大小不超過i - 1的) + dp[i - 1][j](大小爲i的) +dp[i - 1][j - A[i]]
dp[i][j] = dp[i - 1][j] * 2 + dp[i - 1][j - A[i]]

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <deque>
using namespace std;
typedef long long ll;
//__builtin_popcount(n);
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define RI register int
const int MOD = 998244353;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int SZ = 2e5 + 10;
int n,s;
int A[SZ];
ll dp[3050][3050];
int main()
{
	scanf("%d%d",&n,&s);
	for(int i = 1;i <= n;i ++) scanf("%d",&A[i]);
	dp[0][0] = 1;
	for(int i = 1;i <= n;i ++)
		for(int j = 0;j <= s;j ++)
		{
			dp[i][j] =  (dp[i - 1][j] * 2) % MOD ; 
			if(j >= A[i]) dp[i][j] = ( dp[i][j] + dp[i - 1][j - A[i]] ) % MOD;
		}
	printf("%lld",dp[n][s]);
	return 0;
} 

2020.6.1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章