LoginSignup
0
0

More than 3 years have passed since last update.

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

Posted at

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;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0