NCST集训队排位赛(2021-04-25)

A、Sum of 2050

来源:https://codeforces.com/problemset/problem/1517/A
标签:【思维】【简单】
难度:★☆☆☆☆

题目简述

如果一个数是2050*10k(k>=1),则把它称为2050-number,输入一个数n,判断是否为一或多个2050-number相加组成。如果是,输出最少由多少个2050-number组成,否则输出-1。
Input
T:测试组数。
每组测试输入一个n:需要进行判断的数(1<=n<=1018)。
Output
如题
Sample Input

6
205
2050
4100
20500
22550
25308639900

Sample Output

-1
1
2
1
2
36

More Info

In the third case, 4100=2050+2050.
In the fifth case, 22550=20500+2050.

题目思路

首先判断n是不是2050的倍数,不是直接输出-1;是的话,定义p是n/2050,计算p的各位数之和即为答案。

代码(附注释)

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int t;

int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        ll x,ans=0;
        cin>>x;
        if(x%2050==0){
            ll p=x/2050;          
            while(p){//取各位数
            	ans+=p%10;
                p/=10;
            }
            cout<<ans<<endl;
        }
        else cout<<-1<<endl;
    }
	
    return 0;
}

C、Perfectly Imperfect Array

来源:https://codeforces.com/problemset/problem/1514/A
标签:【数学】【思维】
难度:★☆☆☆☆

题目简述

给定一个含有n个元素的a数列,判断a是否存在这样一个子数列:子数列中所有元素的乘积不是完全平方数。如果有,输出YES,否则输出NO。(a的子数列定义为其删掉0或多个元素所形成的数列)
Input
T(1<=T<=100):测试组数。对于每一组:
  n:数列个数。
  n个数:a1,a2....an
Output
如果存在任一非空子数列满足其乘积不是完全平方数,输出"YES",否则输出"NO".
Sample Input

2
3
1 5 4
2
100 10000

Sample Output

YES
NO

More Info

In the first example, the product of the whole array (20) isn't a perfect square.

In the second example, all subsequences have a perfect square product.

题目思路

一开始想复杂了,虽然n很小,模拟也没必要。其实细想就会发现,只有a数列中每个数都是完全平方数,才能输出NO,否则只能输出YES。

代码(附注释)

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int t,n;
double a[105];

int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        int flag=0;
        cin>>n;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            if(sqrt(a[i])-(int)(sqrt(a[i]))!=0)//不是完全平方数
	    flag=1;
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    
    return 0;
}

D、AND 0, Sum Big#

来源:https://codeforces.com/problemset/problem/1514/B
标签:【位运算】【数论】
难度:★★☆☆☆

题目简述

给定两个数n和k,计算出满足长度为n且满足下列条件的数列的数量:
   1.所有的元素都属于[0,2k-1];
   2.所有元素的按位与运算结果为0;
   3.元素和尽可能大;
答案可能很大,对1e9+7取模。
Input
T:测试组数(t<=10)。
每组测试输入一个n,k(1≤n≤105, 1≤k≤20);
Output
输出答案对1e9+7取模的结果
Sample Input

2
2 2
100000 20

Sample Output

4
226732710

More Info

In the first example, the 4 arrays are:
[3,0],
[0,3],
[1,2],
[2,1].

题目思路

做的时候觉得肯定有一个计算的式子,只可惜脑袋太笨没想出来,看了题解竟然是快速幂,有点可惜,可能多试几次就能试出来了。暂且还不知道为什么会是nk%mod。

代码(附注释)

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll n,t,k;

ll ksm(ll a,ll b) //快速幂
{
    ll res=1;
    while(b){
        if(b&1) res=(res*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return res%mod;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>k;
        cout<<ksm(n,k)<<endl;
    }

    return 0;
}

E、Product 1 Modulo N#

来源:https://codeforces.com/problemset/problem/1514/C
标签:【数学】【贪心】【思维】
难度:★★☆☆☆

题目简述

给定一个数n,找到[0,1,2,...,n-1]最长的子序列使子序列中1所有的数的乘积余n等于1。(b是a的子序列即b可由a删除若干个元素得到,可能是所有,规定一个空的序列代表1.)
Input
n (2≤n≤105).
Output
首先输出一个数,代表满足答案的最长序列的长度。
然后下一行输出满足条件的序列(任意一个)。
Sample Input_1

5

Sample Output_1

3
1 2 3

Sample Input_2

8

Sample Output_2

4
1 3 5 7

题目思路

和n的最大公约数为1的(即互质的),相乘结果肯定也和n互质,从1到n循环,把其都打上标记;选择一个数p作为这些数的乘积与n的余数;如果p!=1,则vis[p]=0,舍弃p;前面标记了的就是答案。因为如果p%n不为1,去掉p就相当于所有乘的结果不是选择乘p而是乘1,相应的%n的结果就会变为1。(至于为什么这样是最长的,我也不懂 -_-||| )

代码(附注释)

#include <bits/stdc++.h>
using namespace std;
bool vis[100005];

int main()
{
    int n;
    long long p=1;
    scanf("%d",&n);
    for(int i=1;i<n;i++){
    	if(__gcd(n,i)==1){//与n互质
    		vis[i]=1;
    		p=(p*i)%n;
		}
    }
    if(p!=1) vis[p]=0;//舍弃p
    cout<<count(vis+1,vis+1+n,1)<<endl;
    for(int i=1;i<n;i++){
        if(vis[i]) cout<<i<<' ';
    }
    return 0;
}
上一篇:Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)


下一篇:Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)