Codeforces Round 925 (Div. 3) D-F

Divisible Pairs

你有两个整数 x , y x,y x,y 和一个长为 n n n 的数组 a a a

你需要求出有多少个正整数对 ( i , j ) (i,j) (i,j) 满足:

  • 1 ≤ i < j ≤ n 1 \le i < j \le n 1i<jn
  • a i + a j a_i + a_j ai+aj 可被 x x x 整除
  • a i − a j a_i - a_j aiaj 可被 y y y 整除

t t t 组数据, 1 ≤ t ≤ 1 0 4 , 1 ≤ n ≤ 2 × 1 0 5 , ∑ n ≤ 2 × 1 0 5 , 1 ≤ a i , x , y ≤ 1 0 9 1 \le t \le 10^4 ,1 \le n \le 2 \times 10^5,\sum n \le 2 \times 10^5,1 \le a_i,x,y \le 10^9 1t104,1n2×105,n2×105,1ai,x,y109

思路

因为两个数相减可以被 y 整除,我们可以得到,两个数对 y 进行取模后得到的值一定是相等的,所以我们按照 a i % y a_i\%y ai%y 进行分类,然后去统计满足相加为 x 的情况,我们同样知道,两个数相加可以被 x 整除 ,即为两个数对 x 进行取模后,相加为 x ,所以我们遍历每个值,若当前值为 k ,那么加上 x-k 的出现次数即可。
因为每个数都是取模后的情况,所以对于 0 这种情况计算其本身的数量即可。

#include <bits/stdc++.h>

using namespace std;
const int N = 3e5 + 5;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef array<ll, 3> ar;
int mod = 1e9+7;
const int maxv = 4e6 + 5;
// #define endl "\n"

void solve()
{
    int n,x,y;
    cin>>n>>x>>y;
    vector<ll> a(n);
    for(auto &ai: a) cin>>ai;
    map<int,map<int,int>  > mp;
    ll ans=0;
    for(auto ai: a){
        int c1=ai%x,c2=ai%y;
        if(c1==0) ans+=mp[c2][c1];
        else ans+=mp[c2][x-c1];
        mp[c2][c1]++;
    }
    cout<<ans<<endl;
} 


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    t=1;
    cin>>t;
    while(t--){
        solve();
    }
    system("pause");
    return 0;
}

Anna and the Valentine’s Day Gift

Anna 和 Sasha 在玩游戏,他们有一个长度为 n n n 的数组 a a a

Anna 为先手。两人分别操作:

  • 轮到 Anna 时,她首先需要选择一个 a i a_i ai,并将其按照数位翻转,并省略前导零。例如 42 42 42 翻转为 24 24 24 1580 1580 1580 翻转为 851 851 851。这样操作后, a a a 数组大小不变。
  • 轮到 Sasha 时,他需要选择 a i , a j ( i ≠ j ) a_i, a_j(i \ne j) ai,aj(i=j),并以任意顺序将其连接。例如 2007 2007 2007 19 19 19 可以连接为 200719 200719 200719 192007 192007 192007。这样操作后, a a a 数组大小减一。

玩家不能跳过回合。当 a a a 中只剩下一个整数时,如果它大于等于 1 0 m 10^m 10m,Sasha 获胜。否则 Anna 获胜。

如果 Anna 和 Sasha 都以最优方式操作,谁会获胜?

思路

考虑A对结果的影响,对于有后缀 0 的数字,其操作后会减少后缀 0 个数的长度,其他的数字则没有任何影响。
考虑B对结果的影响,若B想要赢,则其应该尽可能的将后缀 0 个数多的数拼起来。
所以本质上是计算后缀 0 的组数,以及每一组内有多少个后缀 0 。

#include <bits/stdc++.h>

using namespace std;
const int N = 3e5 + 5;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef array<ll, 3> ar;
int mod = 1e9+7;
const int maxv = 4e6 + 5;
// #define endl "\n"



void solve()
{
    int n,m;
    cin>>n>>m;
    int cnt=0;
    vector<int> p;
    vector<int> a(n);
    for(auto &ai: a) cin>>ai;
    for(int i=0;i<n;i++){
        int x=a[i];
        vector<int> tmp;
        while(x){
            tmp.push_back(x%10);
            x/=10;
            cnt++;
        }
        int cur=0;
        for(auto &t:tmp){
            if(t==0) cur++;
            else break;
        }
        p.push_back(cur);
    }
    int sum=0;
    sort(p.begin(),p.end(),greater<int>());
    for(int i=0;i<p.size();i+=2){
        sum+=p[i];
    }
    if(cnt-sum>m){
        cout<<"Sasha"<<endl;
    }
    else cout<<"Anna"<<endl;

} 


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    t=1;
    cin>>t;
    while(t--){
        solve();
    }
    system("pause");
    return 0;
}

Chat Screenshots

给出 n n n 个人进行的聊天室中, k k k 个人做的截图,每个截图中截图者都在第一位置,其后各位按照发帖时间前后确定。判断这些聊天室截图是否自洽(即能确定至少一种可能的发帖顺序)。

思路

如果不自洽,说明出现了环,所以我们只需要对题目给出的进行建边,然后判断是否有环即可。

#include <bits/stdc++.h>

using namespace std;
const int N = 3e5 + 5;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef array<ll, 3> ar;
int mod = 1e9+7;
const int maxv = 4e6 + 5;
// #define endl "\n"

int  tot, dfsn[N], ins[N], low[N];
stack<int> s;

vector<int> e[N];
vector<vector<int>> scc;
vector<int> b(N);
void dfs(int x)
{
	low[x] = dfsn[x] = ++tot, ins[x] = 1, s.push(x);
	for (auto u : e[x])
	{
		if (!dfsn[u])
		{
			dfs(u);
			low[x] = min(low[x], low[u]);
		}
		else if (ins[u])
			low[x] = min(low[x], dfsn[u]);
	}
	if (dfsn[x] == low[x])
	{
		vector<int> c;
		while (1)
		{
			auto t = s.top();
			c.push_back(t);
			ins[t] = 0;
			s.pop();
			b[t] = scc.size();
			if (t == x)
				break;
		}
		// sort(c.begin(), c.end(),greater<ll>());
		scc.push_back(c);
	}
}
void add(int u,int v)
{
    e[u].push_back(v);
}

void solve()
{
    int n,k;
    cin>>n>>k;
    vector<vector<int> > a(k+5,vector<int> (n+5));
    for(int i=1;i<=k;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
        for(int j=2;j<=n-1;j++) add(a[i][j],a[i][j+1]);
    }
    for(int i=1;i<=n;i++){
        if(!dfsn[i]) dfs(i);
    }
    int ok=1;
    for(auto u: scc){
        if(u.size()>1){
            ok=0;
            break;

        }
    }
    if(ok) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    for(int i=0;i<=n;i++){
        e[i].clear();
        dfsn[i]=low[i]=ins[i]=0;
    }
    tot=0;
    scc.clear();
} 


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    t=1;
    cin>>t;
    while(t--){
        solve();
    }
    system("pause");
    return 0;
}
  • 12
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值