AtCoder Beginner Contest 169

A

签到

#include <bits/stdc++.h>
using namespace std;


int main()
{
    int a , b;
    cin >> a >> b;
    cout << a * b << endl;
    return 0;
}

B

签到,用long long

#include <bits/stdc++.h>
using namespace std;


long long a[100010];
int main()
{
    int n;
    cin >> n;
    for( int i = 1; i <= n ; i ++ ) cin >> a[i];
    int cnt = 0;
    for( int i = 1 ; i <= n ; i ++ ) cnt += a[i] == 0 ? 1 : 0 ;
    if( cnt ) cout << "0\n" , exit(0);
    long long p = 1;
    for( int i = 1 ; i <= n ; i ++ )
    {
        if( a[i] <= 1000000000000000000 / p ) p *= a[i];
        else cout <<"-1\n" , exit(0);
    }
    cout << p << endl;
    return 0;
}

C

如果直接用double会溢出,但是题目已经规定浮点数只有两位,所以我们可以先将b乘100,做完乘法在除100

#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main()
{
    ll a , b;
    string s;
    cin >> a >> s;
    b = ( s[0] - '0' ) * 100 + ( s[2] - '0' ) * 10 +( s[3] - '0' );
    cout << ( long long )( a * b / 100LL ) << endl;
    return 0;
}

D

给定N,问有多少z满足条件

  • \(z =p^e\)\(p\)是质数
  • z可以整除N
  • z不重复

每次还要做N/=z;

首先我们可以把n进行质因数分解,对于每一个p,都会有 \(p^1 , p^2 \cdots , p^i\)

找i最大值可以用二分答案即可

#include <bits/stdc++.h>
#define ll long long
#define f( x ) ( x * ( x +1 ) / 2 )
using namespace std;

const int N = 1e6 + 5;
ll n , cnt  , ans;
int power[N] , p[N];
bool v[N];

int main()
{
    cin >> n;
    for( ll i = 2 ; i * i <= n ; i ++ )
    {
        if( n % i ) continue;
        cnt ++;
        while( n % i == 0 ) n /= i , power[ cnt ]++;
    }
    if( n != 1 ) power[ ++ cnt ] = 1 ;
    for( ll i = 1 , l , r , mid , res; i <= cnt ; i ++ )
    {
         l = 1 , r = power[i] , res = 0;
        while( l <= r )
        {

            mid = ( l + r ) >> 1;
            if( f( mid ) <= power[i] ) res = mid , l = mid + 1;
            else r = mid - 1;
        }
        ans += res;
    }
    cout << ans << endl;
    return 0;
}

E

找规律题,A的中位数到B的中位数之间的每一个数都能够取到,如果是奇数公差为一,偶数的公差为0

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;
int n , a[N] , b[N];

int main()
{
    cin >> n;
    for( int i = 1 ; i <= n ; i ++ ) cin >> a[i] >> b[i];
    sort( a + 1 , a + 1 + n ) , sort( b + 1 , b + 1 + n );
    if( n & 1 ) cout << b[ n / 2 + 1 ] - a[ n / 2 + 1 ] + 1 << endl;
    else cout << b[ n /2  ] + b[ n / 2 + 1 ] - a[ n / 2 ] - a[ n/ 2 + 1 ] + 1 << endl;
    return 0;
}
posted @ 2021-11-03 14:47  PHarr  阅读(70)  评论(0编辑  收藏  举报