Help us understand the problem. What is going on with this article?

pairとmapを使いこなす練習 ABC152 D問題

Atcorder152 D問題

手も足も出なかった問題...
pairmapを使えば一気に分かりやすくなった!
https://atcoder.jp/contests/abc152/tasks/abc152_d

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
const ll INF = 1e9 + 7;
typedef pair<int,int> P;// pair型は2つの値の組を表せる

P f(int x){
    int a=x%10; //下一桁
    int b =0;   //上一桁
    while(x){
        b=x;
        x/=10;
    }
    return P(a,b);
}

int main(){
    int n; cin >> n;
    map<P,int> freq; //連想配列の時に用いられる
    for (int i=1; i<=n; i++){
        P p=f(i);
        freq[p]++;
    }

    ll ans=0;
    for (int i=1; i<=n; i++){
        P p=f(i);
        P q(p.second,p.first);
        ans+=freq[q]; //A.firstとB.second, A.secondとB.firstが一致しているのか確認
    }
    cout << ans << endl;
    return 0;
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした