P4942 小凯的数字

题目:P4942 小凯的数字

小凯的数字

题目背景

NOIP2018 原创模拟题T1

NOIP DAY1 T1 or DAY 2 T1 难度

是否发现与NOIP2017 DAY1 T1 有异曲同工之妙

题目描述

小凯有一天突发奇想,写下了一串数字:$\overline{l(l+1)(l+2)...(r-1)r}$

例如:$l=2,r=5$时,数字为:$2345$

$l=8,r=12$时数字为:$89101112$

小凯很喜欢数字 $9$,所以他想问你他写下的数字除以 $9$ 的余数是多少

例如:$l=2,r=5$时,$2345,,mod,,9 = 5$

输入格式

输入格式:

第一行为数字 $Q$,表示小凯有 $Q$ 个问题

第 $2$ 到 $Q+1$ 行,每行两个数字 $l,r$ 表示数字范围

输出格式

输出格式:

对于每行的问题输出一行,一个数字,表示小凯问题的回答

样例 #1

样例输入 #1

2
2 5
8 12

样例输出 #1

5
5

样例 #2

样例输入 #2

3
1 999
123 456
13579 24680

样例输出 #2

0
6
0

提示

样例1解释:$2345,,mod,,9 = 5$   $89101112,,mod,,9 = 5$

30% 数据满足:$Q\leq10;l,r\leq100$

50% 数据满足:$Q\leq100;l,r\leq10000$

70% 数据满足:$Q\leq1000;l,r\leq10^6$

100%数据满足:$Q\leq10000;0<l,r\leq10^{12}$ 且 $l\leq r$

原题链接

思路:
大佬的题解很好
https://www.luogu.com.cn/article/xt6ixoe9

方法一:

点击查看代码
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define pii pair<int,int>
#define inf 0x3f3f3f3f
#define ll long long
#define el '\n'
using namespace std;
const int N = 1e5 + 5;
string str;
int n, m;
void solve()
{
	cin >> n;
	while (n--) {
		ll l, r;
		cin >> l >> r;
		r %= 9;
		l %= 9;
		if (r < l) r += 9;
		m = 0;
		for (int i = l; i <= r; i++) {
			m += i;
		}
		cout << m % 9 << el;
	}
}

int main()
{
	ios;
	solve();
	return 0;
}
posted @ 2024-05-06 12:55  樱岛麻衣的鹿  阅读(1)  评论(0编辑  收藏  举报