CSU 1354 Distinct Subsequences: Calculate the sum (dp) and csusubsequences of different Subsequences.

Source: Internet
Author: User

CSU 1354 Distinct Subsequences: Calculate the sum (dp) and csusubsequences of different Subsequences.

Question link: Click the open link


Description

Give a positive number, count the sum of the distinct subsequence of it, moreover, any subsequence shoshould not contain leading zeroes should t it is zero.
For example, if the number is 1022, the answer is 1 + 0 + 2 + 10 + 12 + 22 + 102 + 122 + 1022 = 1293.

Input

The first line has an integerT, Means there areTTest cases.
For each test case, there is only one line with a positive number, the number of the digits of it is in range [1,105].
The size of the input file will not exceed 5 MB.

Output

For each test case, print the desired answer in one line. Because the answer may be very large, you just need to print the remainder of it divided by 1000000007 instead.

Sample Input

3710221000000001

Sample Output

71293222222223




Ideas:

Dp [I] indicates the sum of subsequences ending with digit I, and num [I] indicates the number of subsequences ending with I.

import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.Iterator;import java.util.LinkedList;import java.util.PriorityQueue;import java.util.Scanner;import java.util.TreeSet;import java.util.Queue;public class Main {static int mod = 1000000007;static int N = 100010;long[] dp = new long[10], num = new long[10];String s;void work() {int T = cin.nextInt();while(T-- > 0) {s = cin.next();int len = s.length();for(int i = 0; i < 10; i++)dp[i] = num[i] = 0;for(int i = 0; i < len; i++){int x = s.charAt(i)-'0';long sum = 0, n = 0;for(int j = 0; j < 10; j++){sum += dp[j];n += num[j];}if(x>0)n++;dp[x] = sum*10%mod + n*x%mod;num[x] = n%mod;}long ans = 0;for(int i = 0; i < 10; i++)ans = (ans + dp[i])%mod;out.println(ans);}}Main() {cin = new Scanner(System.in);out = new PrintWriter(System.out);}public static void main(String[] args) {Main e = new Main();e.work();out.close();}public Scanner cin;public static PrintWriter out;int max(int x, int y) {return x > y ? x : y;}int min(int x, int y) {return x < y ? x : y;}double max(double x, double y) {return x > y ? x : y;}double min(double x, double y) {return x < y ? x : y;}long max(long x, long y) {return x > y ? x : y;}long min(long x, long y) {return x < y ? x : y;}static double eps = 1e-8;int abs(int x) {return x > 0 ? x : -x;}double abs(double x) {return x > 0 ? x : -x;}long abs(long x) {return x > 0 ? x : -x;}boolean zero(double x) {return abs(x) < eps;}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.