PAT (Top Level) Practise 1018 Subnumbers (35)

1018. Subnumbers (35)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CAO, Peng

Given a positive integer N, let us define a "subnumber" of N as a consecutive number of digits NOT starting with 0. For example if N = 1021, it has 7 subnumbers, namely, 1, 10, 102, 1021, 2, 21 and 1 (again). Here is your task: please calculate the sum of all the subnumbers of N. For 1021, the sum is 1+10+102+1021+2+21+1 = 1158. Since the result may be very large, output the answer modulo 1000000007 (109 + 7) please.

Input Specification:

Each input file contains one test case, which gives the integer N (0 < N < 10100000) in a line.

Output Specification:

Print in a line the sum of all N's subnumbers (modulo 1000000007).

Sample Input:
1234567890123456789
Sample Output:
332876913
统计每个数字的贡献即可
#include 
#include
#include  
#include      
#include   
#include  
#include  
#include      
#include    
#include  
#include      
#include  
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))      
#define rep(i,j,k) for(int i=j;i<=k;i++)      
#define per(i,j,k) for(int i=j;i>=k;i--)      
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])      
#define inone(x) scanf("%d",&x)      
#define intwo(x,y) scanf("%d%d",&x,&y)      
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)   
#define lson x<<1,l,mid  
#define rson x<<1|1,mid+1,r  
#define mp(i,j) make_pair(i,j)  
#define ff first  
#define ss second  
typedef long long LL;
typedef pair pii;
const int low(int x) { return x&-x; }
const double eps = 1e-6;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5+ 500;
int T, n, g[N];
char s[N];

int main() {
  while (~scanf("%s", s)) {
    int len = strlen(s);
    g[0] = 0;
    for (int i = 1; i <= len; i++) {
      g[i] = (10LL * g[i - 1] + 1) % mod;
    }
    int sum = 0, ans = 0;
    for (int i = 0; i < len; i++) {
      sum += s[i] != '0';
      ans = (ans + 1LL * (s[i] - '0') * sum % mod * g[len - i]) % mod;
    }
    printf("%d\n", ans);
  }
  return 0;
}

你可能感兴趣的:(PAT顶级)