ZOJ-4137 Digit Product

题目描述:

Define the "digit product" f(x) of a positive integer x as the product of all its digits. For example, f(1234)=1×2×3×4=24, and f(100)=1×0×0=0.

Given two integers l and r, please calculate the following value: (∏i=lr​ f(i))mod(109+7) In case that you don't know what ∏ represents, the above expression is the same as (f(l)×f(l+1)×⋯×f(r))mod(1e9+7)

Input

There are multiple test cases. The first line of the input contains an integer TT(about 10^5), indicating the number of test cases. For each test case:

The first and only line contains two integers l and r (1≤l≤r≤109), indicating the given two integers. The integers are given without leading zeros.

Output

For each test case output one line containing one integer indicating the answer.

Sample

Input

2
1 9
97 99

Output

362880
367416

Hint

For the first sample test case, the answer is 9! mod (10^9+7) = 3628809.

For the second sample test case, the answer is (f(97)×f(98)×f(99))mod(1e9+7)=(9×7×9×8×9×9)mod(1e9+7)=367416.

Time limit

1000 ms

Mem limit

65536 kB

OS

Linux

Author

WENG, Caizhi

Spoilers

Hide

题目大意:一个函数f(x)的结果是x的各位数相乘,给你l和r,让你求

 思路分析:分析可知,一旦[l,r]中的某个数的某一位是0,那么结果肯定为0,十进制满10进1,所以,只要l和r中包含10的个数不相同,说明l和r之间一定有一个数,它的末位为0。

代码:

#include<iostream>
#include<vector>
const int MAX = 1e9 + 7;
using namespace std;
int main() {
	int t;
	cin>>t;
	while (t--) {
		int l, r;
		scanf("%d%d", &l, &r);
		if (l / 10 != r / 10) //10的个数不同,一定有0,直接输出 
		{
			printf("0\n");
		} else {
			int temp = l, qm = 1;//除末尾数字外其他各位的乘积 
			long long int sum = 1;
			temp = temp / 10;
			if (temp == 0)//特殊情况,只有一位 
				qm = 1;
			else {
				while (temp / 10 != 0) {
					qm *= (temp % 10);
					temp = temp / 10;
				}
				qm *= temp;
			}
			for (int i = l; i <= r; i++) {
				int ys = i % 10;
				sum = sum * qm * ys % (MAX);
			}
			printf("%lld\n", sum);
		}
	}
}

有用的英文单词:无

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值