Codeforces Round #191 (Div. 2) C. Magic Five rapid power of the proportional Series

Source: Internet
Author: User

Question Link

This question seems like the basic idea of POJ3233 is the same.

This question should be obtained using a fast power. If there are many proportional series of items, Division numbers should be included in the summation formula. Therefore, mod cannot be taken directly, and fast rice conversion should be performed.

For example, sum = 2 ^ 1 + 2 ^ 2 + 2 ^ 3 + 2 ^ 4 + 2 ^ 5 + 2 ^ 6 + 2 ^ 7 .......

N items in total

This is the formula if n % 2 = 0 T (n) = T (n/2) + T (n/2) * 2 ^ (n/2 );

If n % 2 = 1 T (n) = T (n/2) + T (n/2) * 2 ^ (n/2) + 2 ^ n;

For this question, we first calculate the sum of the given cyclic bits as the basis, and then use the formula above the quick power to solve the problem,

I changed this question many times after the competition, especially the storage of _ int64 in its operation.

Below I will post my code ~~~ Write is a bit messy.

# Include <stdio. h> # include <iostream> # include <string. h> # include <algorithm> # include <math. h> # include <stdlib. h> using namespace std; const _ int64 mod = 1000000007 ;__ int64 all; int Pow (_ int64 n, int num) {// fast power num ^ n if (n = 0) return 1; if (n = 1) return num; _ int64 tmp = Pow (n/2, num) % mod; tmp = tmp * tmp % mod; if (n % 2 = 1) tmp = tmp * num % mod; return (int) tmp ;} __int64 len; int q; int fun (int n) {// the above-mentioned core formula if (n = 1) return al L; _ int64 tmp = fun (n/2); tmp = (tmp + tmp * Pow (n/2, q) % mod; if (n % 2 = 1) tmp = (tmp + Pow (n-1) * len, 2) * all % mod) % mod; return (int) tmp ;} char a [2000000]; int main () {int n, ans; while (scanf ("% s", )! = EOF) {cin> n; ans = 0; len = strlen (a); q = Pow (len, 2) % mod; all = 0; for (int I = 0; I <len; I ++) {if (a [I] = '0' | a [I] = '5 ') {all = (all + Pow (I, 2) % mod ;}// cout <I <"" <ans <endl ;} // cout <"all =" <all <endl; ans = fun (n); cout <ans <endl;} return 0 ;}

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.