"Mathematics" CSU 1810 Reverse (2016 Hunan province 12th session of computer Program design Competition)

Source: Internet
Author: User

Topic Links:

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810

Main topic:

A decimal number of length n, R (I,J) indicates that the number after flipping the position I to the first J is the MoD 109+7

Topic Ideas:

Mathematical

This is a different way of thinking, to see how each number can contribute to the answer. (Can push 01,10,001,010,100 ...) can also write a violent like me to play a table within 10 to see the law)

Now consider the case where the number of position I is 1 (this is the last number on the line). It can be found that the contribution is symmetrical (1 and 1 of the n-i+1 are the same as the number of times in the other position, so only consider the I<=N/2 case)

The 1 of the bit I is left in the case of I, there are three, I-left interval exchange, I-right interval exchange, with I-centered interval exchange. The number of exchanges can be obtained through O (1).

And I can exchange to other positions of the answer is also good to push, exchange to the first only interval [1,i], swap to the second bit has the interval [1,i+1],[2,i], third ...

It can be found that the number of exchanges to the first I is,... i-1,i,i,..., i,i,i-1,..., 2,1 (the latter half and the first half are symmetrical) (from 1 to i,i flat, from I to 1)

The effect of the ascending sequence of the preceding and subsequent sequences on the final answer can be computed by preprocessing (the following is: 1+2*10+3*100 ... before, remember to take a power of 10), and the middle level can be prefixed and calculated (10 power interval and *i)

So this problem can be done. Enumerate each bit of I and add its contribution to the answer as above.

1 //2 //by Coolxxx3 //#include <bits/stdc++.h>4#include <iostream>5#include <algorithm>6#include <string>7#include <iomanip>8#include <map>9#include <stack>Ten#include <queue> One#include <Set> A#include <bitset> -#include <memory.h> -#include <time.h> the#include <stdio.h> -#include <stdlib.h> -#include <string.h> - //#include <stdbool.h> +#include <math.h> - #defineMin (a) < (b) ( A):(B)) + #defineMax (a) (a) > (b)? ( A):(B)) A #defineABS (a) ((a) >0? ( A):(-(a))) at #defineLowbit (a) (a& (a)) - #defineSqr (a) ((a) * (a)) - #defineSwap (a) (a) ^= (b), (b) ^= (a), (a) ^= (b)) - #defineMem (A, B) memset (A,b,sizeof (a)) - #defineEPS (1E-8) - #defineJ 10000 in #defineMoD 1000000007 - #defineMAX 0x7f7f7f7f to #definePI 3.14159265358979323 + #defineN 100004 - using namespacestd; thetypedefLong LongLL; * intCas,cass; $ intN,m,lll,ans;Panax Notoginseng LL Aans; - LL E[n],sum[n],l[n],r[n]; the LL A; + CharS[n]; A intMain () the { + #ifndef Online_judge - //freopen ("1.txt", "R", stdin); $ //freopen ("2.txt", "w", stdout); $     #endif -     inti,j,k; - LL x, y; the //for (scanf ("%d", &cass); cass;cass--) - //for (scanf ("%d", &cas), cass=1;cass<=cas;cass++)Wuyi //while (~scanf ("%s", s)) the      while(~SCANF ("%d",&N)) -     { Wuaans=0; -scanf"%s", s); Aboutn=strlen (s); $e[n]=1; e[n+1]=0; -          for(i=n-1; i;i--) e[i]= (e[i+1]*Ten)%MoD; -r[n]=1; r[n+1]=0; -          for(i=n-1; i+i>=n;i--) r[i]= (r[i+1]+e[i]* (n-i+1))%MoD; Al[1]=e[1];l[0]=0; +          for(i=2; i+i<=n+2; i++) l[i]= (l[i-1]+e[i]*i)%MoD; thesum[1]=e[1]; -          for(i=2; i<=n;i++) sum[i]=sum[i-1]+E[i]; $          the          for(i=1; i<=n;i++) the         { thea=s[i-1]-'0'; the             if(a==0)Continue; -x=i;y=n-i+1; in             if(x>y) Swap (x, y); theAans= (Aans+a*l[min (x, y)-1])%MoD; theAans= (Aans+a*r[max (x, y) +1])%MoD; AboutAans= (aans+ (a*x* (sum[y]-sum[x-1])) (%MOD)%MoD; theAans= (aans+ (e[i]*a)%mod* (x* (x1)/2+y* (y1)/2)%mod)%MoD; the         } theprintf"%lld\n", Aans); +     } -     return 0; the }Bayi /* the // the  - // - */
View Code

"Mathematics" CSU 1810 Reverse (2016 Hunan province 12th session of computer Program design Competition)

Related Article

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.