CodeForces Round #191 (327C)-Magic Five rapid power Modulo for sum of proportional Series

Source: Internet
Author: User

I have done this kind of problem a long time ago .. that's because it's been too long .. I have been thinking about this question for a long time .. the sum formula of the Equality ratio is introduced, and Division operations are involved, so it is impossible to quickly modulo the power...

 

Just think of the Rapid power modulo of the proportional series ....

Returns the sum of the proportional series T [n] of k. when n is an even number .. T [n] = T [n/2] + pow (k, n/2) * T [n/2]

N is an odd number... T [n] = T [n/2] + pow (k, n/2) * T [n/2] + value of the nth Number of the proportional Series

For example, 1 + 2 + 4 + 8 = (1 + 2) + 4*(1 + 2)

1 + 2 + 4 + 8 + 16 = (1 + 2) + 4*(1 + 2) + 16

 


Program:

 

# Include <iostream> # include <stdio. h> # include <string. h> # include <set> # include <algorithm> # include <cmath> # define oo 1000000007 # define ll long # define pi acos (-1.0) # define MAXN 505 using namespace std; char s [100004]; ll m; ll POW (ll a, ll k) {ll x, ans = 1; x =; while (k) {if (k % 2) ans = (ans * x) % oo; x = (x * x) % oo; k/= 2 ;} return ans;} ll T (ll n, ll t) {if (n = 1) return t; ll data = T (n/2, t ); data = (data + data * POW (m, N/2) % oo; if (n % 2) data = (data + POW (m, (n-1) * t) % oo; return data ;} int main () {int k, I; ll ans, x, len; while (~ Scanf ("% s", s) {scanf ("% d", & k); len = strlen (s); m = POW (2, len ); ans = 0; x = 1; for (I = 0; I <len; I ++) {if (s [I] = '0' | s [I] = '5') ans = (ans + x) % oo; // do not do it every time .. add up x = (x * 2) % oo;} ans = T (k, ans); // only make one printf ("% I64d \ n", ans );} return 0 ;}

 

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.