roycelang24's blog

By roycelang24, history, 3 weeks ago, In English

Your text to link here...

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n,k,q,m;
int in[maxn];
vector<int> adj[maxn],a[maxn];

//建图,判断该图是否存在拓扑序
void solve()
{   
    cin >> n >> k;
    for(int i = 1;i<=k;i++) a[i].clear();
    for(int i = 1;i<=n;i++) adj[i].clear();
    for(int i = 1;i<=k;i++)
    {
        a[i].resize(n+2);
        for(int j = 1;j<=n;j++) cin >> a[i][j],in[a[i][j]] = 0;
    }
    if(k==1)
    {
        cout << "YES" << endl;
        return;
    }
    //邻接表存图
    for(int i = 1;i<=k;i++)
    {
        for(int j = 3;j<=n;j++)
        {
            adj[a[i][j-1]].push_back(a[i][j]);
            in[a[i][j]]++;
        }
    }
    //拓扑排序核心代码
    queue<int> q;
    for(int i = 1;i<=n;i++)
    {
        if(in[i]==0) q.push(i);
    }
    while(!q.empty())
    {
        int temp = q.front();
        q.pop();
        for(auto x:adj[temp]) 
        {
            in[x]--;
            if(in[x]==0) q.push(x);
        }
    }

    bool flag = 1;
    for(int i = 1;i<=n;i++)
    {
        if(in[i]!=0)
        {
            flag = 0;
            break;
        }
    }
    if(!flag) cout << "NO" << endl;
    else cout << "YES" << endl;
}

signed main()
{
    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }

    return 0;
}

Full text and comments »

  • Vote: I like it
  • -27
  • Vote: I do not like it

By roycelang24, history, 3 weeks ago, In English

Your text to link here... _(a+b) mod x = (a mod x+b mod x) mod _ 对于本题: (ai+aj) mod x = (ai mod x+aj mod x) mod x = 0 即ai mod x+aj mod x = 0 / x 变形为->ai mod x = (x-aj mod x) mod x (ai mod x+aj mod x=0时ai mod x = aj mod x = 0,依然满足上式)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n,a[maxn],x,y;

void solve()
{
    cin >> n >> x >> y;
    for(int i = 1;i<=n;i++) cin >> a[i];
    long long ans = 0;
    map<pair<int,int>,int> mp;
    for(int i = 1;i<=n;i++)
    {
        int res1 = (x-a[i]%x)%x,res2 = a[i]%y;
        pair<int,int> p={res1,res2};
        ans += mp[p];
        p = {a[i]%x,a[i]%y};
        mp[p]++;
    }
    cout << ans << endl;
}

signed main()
{
    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }

    return 0;
}

Full text and comments »

  • Vote: I like it
  • -36
  • Vote: I do not like it