Codeforces_803

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces_803相关的知识,希望对你有一定的参考价值。

A. 填k个1,使矩阵主对角线对称,相同情况选择上面1数量多的。

技术分享
#include<bits/stdc++.h>
using namespace std;

int n,k,a[105][105] = {0};

int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k;
    if(k > n*n)
    {
        cout << - 1 << endl;
        return 0;
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            if(a[i][j])   continue;
            if(k >= 1 && i == j)
            {
                k--;
                a[i][j] = 1;
            }
            else if(k >= 2)
            {
                k -= 2;
                a[i][j] = 1;
                a[j][i] = 1;
            }
        }
    }
    if(k != 0)
    {
        cout << -1 << endl;
        return 0;
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)   cout << a[i][j] << " ";
        cout << endl;
    }
    return 0;
}
View Code

B.两个方向各跑一次,更新最邻近的0位置就可以了。

技术分享
#include<bits/stdc++.h>
using namespace std;

int n,a[200005],l[200005],r[200005];

int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 1;i <= n;i++)   cin >> a[i];
    int last = -1e9;
    for(int i = 1;i <= n;i++)
    {
        if(a[i] == 0)   last = i;
        l[i] = i-last;
    }
    last = 1e9;
    for(int i = n;i >= 1;i--)
    {
        if(a[i] == 0)   last = i;
        r[i] = last-i;
    }
    for(int i = 1;i <= n;i++)   cout << min(l[i],r[i]) << " ";
    cout << endl;
    return 0;
}
View Code

C.因为和为n,所以可以是某个最小的初始小序列的某倍数为n,达到gcd最大,枚举n的每一个因子即可。

技术分享
#include<bits/stdc++.h>
using namespace std;

long long n,k;

int main()
{
    ios::sync_with_stdio(fals#include<bits/stdc++.h>
using namespace std;

long long n,k;

int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k;
    if(log(n*2) < log(k)+log(k+1)-1e-9)
    {
        cout << -1 << endl;
        return 0;
    }
    long long t = k*(k+1)/2,maxx = 1;
    for(long long i = 1;i*i <= n;i++)
    {
        if(n%i) continue;
        {
            if(i*t <= n)    maxx = max(maxx,i);
            if(t <= i)  maxx = max(maxx,n/i);
        }
    }
    for(long long i = 1;i < k;i++)   cout << i*maxx << " ";
    cout << n-maxx*k*(k-1)/2 << endl;
    return 0;
}
e);
    cin >> n >> k;
    if(log(n*2) > log(k)+log(k+1)-1e-9)
    {
        cout << -1 << endl;
        return 0;
    }
    long long t = k*(k+1)/2,maxx = 1;
    for(long long i = 1;i*i <= n;i++)
    {
        if(n%i) continue;
        {
            if(i*t <= n)    maxx = max(maxx,i);
            if(n/i*t <= n)  maxx = max(maxx,n/i);
        }
    }
    for(int i = 1;i <= n;i++)   cout << i*maxx << " ";
    cout << n-maxx*k*(k-1)/2 << endl;
    return 0;
}
View Code

D.直接划分成每个最小单词的字符个数,然后二分答案。

技术分享
#include<bits/stdc++.h>
using namespace std;

int n,cnt = 1,a[1000005] = {0};
string s;

bool ok(int x)
{
    int cntt = 0,now = 0;
    for(int i = 1;i <= cnt;i++)
    {
        if(a[i] > x)  return 0;
        now += a[i];
        if(now > x)
        {
            cntt++;
            now = a[i];
        }
    }
    if(now > 0) cntt++;
    if(cntt <= n)   return 1;
    return 0;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin,s);
    for(int i = 0;i < s.length();i++)
    {
        a[cnt]++;
        if(s[i] ==   || s[i] == -)    cnt++;
    }
    int l = 1,r = 1e6;
    while(l < r)
    {
        int mid = (l+r)/2;
        if(ok(mid)) r = mid;
        else    l = mid+1;
    }
    cout << l << endl;
    return 0;
}
View Code

E.dp[i][j]表示到i轮状态为j的可能,j可以用赢的量表示,因为会有负值,可以设置一个偏移量,因为要记录路径,直接把dp[i][j]用来记录上一状态,然后逆向输出路径就可以了。

技术分享
#include<bits/stdc++.h>
using namespace std;

int n,k,a[1005][2005];
string s;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k >> s;
    memset(a,-1,sizeof(a));
    a[0][n] = 0;
    for(int i = 0;i < n;i++)
    {
        for(int j = n-k+1;j <= n+k-1;j++)
        {
            if(a[i][j] == -1)   continue;
            if(s[i] == ? || s[i] == W)  a[i+1][j+1] = j;
            if(s[i] == ? || s[i] == L)  a[i+1][j-1] = j;
            if(s[i] == ? || s[i] == D)  a[i+1][j] = j;
        }
    }
    if(a[n][n-k] == -1 && a[n][n+k] == -1)
    {
        cout << "NO" << endl;
        return 0;
    }
    int now = n-k;
    if(a[n][n-k] == -1)   now = n+k;
    for(int i = n;i >= 1;i--)
    {
        if(a[i][now] == now-1)  s[i-1] = W;
        else if(a[i][now] == now+1) s[i-1] = L;
        else    s[i-1] = D;
        now = a[i][now];
    }
    cout << s << endl;
    return 0;
}
View Code

F.gcd为1的序列数较难求得,我们可以求gcd不为1的序列数,预处理统计每个数的约数,求得所有约数的个数,其中,k个数中的选择情况有2^k-1种,容斥原理求得答案。

技术分享
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std;

int n,cnt[100005] = {0},ans[100005],two[100005];

int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    while(n--)
    {
        int x;
        cin >> x;
        for(int i = 1;i*i <= x;i++)
        {
            if(x%i == 0)
            {
                cnt[i]++;
                if(i*i != x)    cnt[x/i]++;
            }
        }
    }
    two[0] = 1;
    for(int i = 1;i <= 100000;i++)  two[i] = two[i-1]*2%MOD;
    for(int i = 1;i <= 100000;i++)  ans[i] = two[cnt[i]]-1;
    for(int i = 100000;i >= 1;i--)
    {
        for(int j = i*2;j <= 100000;j += i) ans[i] = (ans[i]-ans[j]+MOD)%MOD;
    }
    cout << ans[1] << endl;
    return 0;
}
View Code

 

以上是关于Codeforces_803的主要内容,如果未能解决你的问题,请参考以下文章

codeforces 803B Distances to Zero

codeforces 803

CodeForces 803D Magazine Ad

Maximal GCD CodeForces - 803C (数论+思维优化)

CodeForces 803F Coprime Subsequences 莫比乌斯,容斥

Codeforces 803E--Roma and Poker (DP)