Contest2240 - 抗击疫情,从我做起--大中小学生联合训练赛第四十九场

问题 L: Handstand 2
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]

题目描述

Given is a positive integer N.Find the number of pairs (A,B) of positive integers not greater than
N that satisfy the following condition:
·When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.
Constraints
·1≤N≤2×105
·All values in input are integers.

输入

Input is given from Standard Input in the following format:

N

输出

Print the answer.

样例输入 Copy

【样例1】
25
【样例2】
1
【样例3】
100
【样例4】
2020
【样例5】
200000

样例输出 Copy

【样例1】
17
【样例2】
1
【样例3】
108
【样例4】
40812
【样例5】
400000008

提示

样例1解释
The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5),
(6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

  • 题意就是找到两个数满足一个数的第一位是另一个数的最后一位,另一个数的第一位是这个数的最后一位;

  • 思路分析:遍历每个数 得出首位和末尾的数字,然后模拟组合。
    代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn=2e6+1010;
#define inf 0x3f3f3f3f
const int mod=1e9+7;

ll f[maxn],a[maxn],b[maxn],n,m,t,flag,temp,sum,x,y;
string str,s;
ll dp[100][100];

ll find(ll x){//首位的大小 
    while(x){
        if(x/10==0){
            return x;
        }
        x/=10;
    }
}

ll gcd(ll a,ll b){
	while(b){
		ll temp=a%b;
		a=b;
		b=temp;
	}
	return a;
}

int main()
{
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		dp[find(i)][i%10]++;//首位末尾大小 
	}
	for(int i=1;i<=9;i++)
		for(int j=1;j<=9;j++)
			sum+=dp[i][j]*dp[j][i];//表示可能性
	
	cout<<sum<<endl; 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45911397/article/details/105011428