Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define dbg(i,j) cout<<"I am "<<i<<" = "<<endl<<j<<endl;
- #define dbr(name,a) cout<<name<<endl;for(auto x:a)cout<<x<<" ";cout<<endl;
- #define DBR(name,a) cout<<name<<endl;for(auto x:a){ for(auto y:x)cout<<y<<" ";cout<<endl;}
- #define dbmp(name,a) cout<<name<<endl;for(auto x:a){ cout<<x.first<<"\t"<<x.second<<endl;}
- #define dbp(name,a) cout<<name<<endl;cout<<a.first<<"\t"<<a.second<<endl;
- #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- using namespace std;
- typedef long long int big;
- typedef long double fig;
- big power(big x, big y, big p)
- {
- big res = 1; // Initialize result
- x = x % p; // Update x if it is more than or
- // equal to p
- while (y > 0)
- {
- // If y is odd, multiply x with result
- if (y & 1)
- res = (res*x) % p;
- // y must be even now
- y = y>>1; // y = y/2
- x = (x*x) % p;
- }
- return res;
- }
- bool prime[100005];
- void SieveOfEratosthenes(big n)
- {
- // Create a boolean array "prime[0..n]" and initialize
- // all entries it as true. A value in prime[i] will
- // finally be false if i is Not a prime, else true.
- //bool prime[n+1];
- memset(prime, true, sizeof(prime));
- for (int p=2; p*p<=n; p++)
- {
- // If prime[p] is not changed, then it is a prime
- if (prime[p] == true)
- {
- // Update all multiples of p greater than or
- // equal to the square of it
- // numbers which are multiple of p and are
- // less than p^2 are already been marked.
- for (int i=p*p; i<=n; i += p)
- prime[i] = false;
- }
- }
- // Print all prime numbers
- /*for (int p=2; p<=n; p++)
- if (prime[p])
- cout << p << " ";*/
- }
- big mod(vector<big> num, big a)
- {
- // Initialize result
- big res = 0;
- // One by one process all digits of 'num'
- for (int i = 0; i < num.size(); i++)
- res = (res*10 + num[i]) %a;
- return res;
- }
- big mod(big a,big b){
- return (a%b+b)%b;
- }
- big arr[]={0,1,3,6,10,15,21,28,36,45,45,46,48,51,55,60,66,73,81,90};
- int main(){
- //freopen("i.txt","r",stdin);
- //freopen("o.txt","w",stdout);
- big t=1;
- cin>>t;
- while(t--){
- big n1,n2;
- big p=1000000007;
- big INV=700000005;
- big FF=45;
- /*for(big i=0;i<n;i++){
- cin>>num[i];
- }
- cout<<mod(num,1000000007)<<endl;*/
- string l;
- string r;
- cin>>n1>>l;
- cin>>n2>>r;
- big flag=1;
- for(big i=r.size()-1;i>=0 && flag;i--){
- if(r[i]=='9'){
- r[i]='0';
- }
- else{
- r[i]=(char)(r[i]+1);
- flag=0;
- }
- }
- if(flag){
- r="1"+r;
- n2++;
- }
- string t;
- for(big i=0;i<(n2-n1);i++){
- t.push_back('0');
- }
- l=t+l;
- //cout<<l<<endl;
- //cout<<r<<endl;
- big n=n2;
- vector<big> num(n);
- vector<big> cd(n);
- for(big i=0;i<n;i++){
- cd[i]=(big)(r[i]-l[i]);
- }
- //dbr("cd",cd);
- vector<big> tens(n);
- tens[0]=1;
- for(big i=1;i<n;i++){
- tens[i]=mod((tens[i-1]*10),p);
- }
- //dbr("tens",tens);
- vector<big> cdmt(n);
- for(big i=0;i<n;i++){
- cdmt[i]=mod(cd[i],10);
- }
- //dbr("cdmt",cdmt);
- vector<big> cdmp(n);
- for(big i=0;i<n;i++){
- cdmp[i]=mod(cd[i],p);
- }
- //dbr("cdmp",cdmp);
- vector<big> diff(n);
- diff[0]=cdmp[0];
- for(big i=1;i<n;i++){
- diff[i]=mod((mod(diff[i-1]*10,p)+cdmp[i]),p);
- }
- //dbr("diff",diff);
- vector<big> key(n);
- for(big i=0;i<n;i++){
- big temp=mod(mod((diff[i]+mod((-1)*cdmt[i],p)),p)*INV,p);
- // dbg("temp",temp);
- key[i]=mod(temp*45,p);
- big start=(big)(l[i]-'0');
- temp=(start==0)?arr[cdmt[i]-1]:(arr[start+cdmt[i]-1]-arr[start-1]);
- temp=mod(temp,p);
- key[i]=mod((key[i]+temp),p);
- }
- //dbr("key",key);
- vector<big> res(n);
- for(big i=0;i<n;i++){
- res[i]=mod((key[i]*tens[n-i-1]),p);
- }
- //dbr("res",res);
- vector<big> sub1(n);
- sub1[n-1]=(big)(l[n-1]-'0');
- for(big i=n-2;i>=0;i--){
- sub1[i]=mod((sub1[i+1]+mod(big(l[i]-'0')*tens[n-i-1],p)),p);
- }
- //dbr("sub1",sub1);
- vector<big> sub2(n);
- sub2[n-1]=(big)(r[n-1]-'0');
- for(big i=n-2;i>=0;i--){
- sub2[i]=mod((sub2[i+1]+mod(big(r[i]-'0')*tens[n-i-1],p)),p);
- }
- //dbr("sub2",sub2);
- for(big i=0;i<n-1;i++){
- res[i]=mod((res[i]+mod(((-1)*(big)(l[i]-'0')*sub1[i+1]),p)),p);
- }
- //dbr("res",res);
- for(big i=0;i<n-1;i++){
- res[i]=mod((res[i]+mod(((1)*(big)(r[i]-'0')*sub2[i+1]),p)),p);
- }
- //dbr("res",res);
- //cout<<"Normal result= "<<mod(res,p)<<endl;
- for(big i=1;i<n;i++){
- res[i]=mod((res[i]+mod((-1)*(key[i-1]*tens[n-i-1]),p)),p);
- }
- //dbr("res_after_normal",res);
- for(big i=1;i<n-1;i++){
- if(l[i]<l[i-1]){
- ;
- }
- else if(l[i]==l[i-1]){
- res[i]=mod((res[i]+mod(sub1[i+1]*(l[i]-'0'),p)),p);
- }
- else{
- res[i]=mod((res[i]+mod((l[i-1]-'0')*tens[n-i-1],p)),p);
- }
- }
- if(l[n-1]>l[n-2]){
- res[n-1]=mod((res[n-1]+mod(1*(l[n-2]-'0'),p)),p);
- }
- //dbr("res_after_first_correction",res);
- for(big i=1;i<n-1;i++){
- if(r[i]<r[i-1]){
- ;
- }
- else if(r[i]==r[i-1]){
- res[i]=mod((res[i]+mod(sub2[i+1]*(r[i]-'0')*(-1),p)),p);
- }
- else{
- res[i]=mod((res[i]+mod((r[i-1]-'0')*tens[n-i-1]*(-1),p)),p);
- }
- }
- if(r[n-1]>r[n-2]){
- res[n-1]=mod((res[n-1]+mod((-1)*(r[n-2]-'0'),p)),p);
- }
- //dbr("res_after_second_correction",res);
- cout<<mod(res,p)<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment