Codeforces Round #475 (Div. 2) C. Alternating Sum (Math, Inverse Elements)

Link: http://codeforces.com/contest/964/problem/C

Question meaning: In fact, I didn't understand the meaning of the question very much, and it is difficult to think of it like this. I still wrote the code myself after reading the answer to the question! ~!

Problem solving link http://codeforces.com/blog/entry/58991

If it is to find the inverse element, then it is very simple, but the form of the question has changed a little, and it is difficult for me to think of deriving it like this! ~

At the beginning, I didn't expect that the following summation equation can be directly used for the summation formula of the proportional number sequence. I made myself TLE once, and after thinking for a long time, I found the problem; what is more tragic is that I am using the proportional number sequence to sum. When formulating the formula, I have to ask for q! ~! q=(b/a)^k; the result was calculated by me as q=(b^k)/a. WA made a lot of hair

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll mod=1e9+9;
 5 const int maxk=1e5+10;
 6 ll n,a,b,k;
 7 char s[maxk];
 8 ll Q_pow(ll x,ll n){
 9     ll ans=1;
10     while(n){
11         if(n&1){
12             ans=ans*x%mod;
13         }
14         x=x*x%mod;
15         n>>=1;
16     }
17     return ans;
18 }
19 ll Get_Z(ll a,ll b){
20     ll ans=0;
21     for(int i=0;i<=k-1;i++){
22         ll ta=Q_pow(a,n-i);
23         ll tb=Q_pow(b,i);
24         if(s[i]=='+')
25             ans=(ans+ta*tb%mod+mod)%mod;
26         else
27             ans=(ans-ta*tb%mod+mod)%mod;
28     }
29     return ans;
30 }
31 int main()
32 {
33     while(cin>>n>>a>>b>>k){
34         cin>>s;
35         ll Z=Get_Z(a,b);
36         ll B=Q_pow(b,k);
37         ll A=Q_pow(a,mod-2);
38          A=Q_pow(A,k);
39         ll q=A*B%mod;
40 
41         ll sum=0;
42         ll qn=(n+1)/k-1;
43         if(q==1)
44             sum=(qn+1)%mod;
45         else
46             sum=(Q_pow(q-1,mod-2)*(Q_pow(q,qn+1)-1))%mod;
47         cout<<(Z*sum+mod)%mod<<endl;
48     }
49     return 0;
50 }
 
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll mod=1e9+9;
 5 const int maxk=1e5+10;
 6 ll n,a,b,k;
 7 char s[maxk];
 8 ll Q_pow(ll x,ll n){
 9     ll ans=1;
10     while(n){
11         if(n&1){
12             ans=ans*x%mod;
13         }
14         x=x*x%mod;
15         n>>=1;
16     }
17     return ans;
18 }
19 ll Get_Z(ll a,ll b){
20     ll ans=0;
21     for(int i=0;i<=k-1;i++){
22         ll ta=Q_pow(a,n-i);
23         ll tb=Q_pow(b,i);
24         if(s[i]=='+')
25             ans=(ans+ta*tb%mod+mod)%mod;
26         else
27             ans=(ans-ta*tb%mod+mod)%mod;
28     }
29     return ans;
30 }
31 ll Get_q(ll a,ll b){
32     ll t=(n+1)/k;
33     ll ans=0;
34     ll A=Q_pow(a,mod-2);
35     for(ll i=0;i<t;i++){
36         ll tb=Q_pow(b,i*k);
37         ll ta=Q_pow(A,i*k);
38         ans=(ans+tb*ta%mod)%mod;
39     }
40     return ans;
41 }
42 int main()
43 {
44     while(cin>>n>>a>>b>>k){
45         cin>>s;
46         ll t1=Get_Z(a,b);
47         ll t2=Get_q(a,b);
48         //cout<<t1<<endl;
49         //cout<<t2<<endl;
50         cout<<t1*t2%mod;
51     }
52     return 0;
53 }
error code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324996754&siteId=291194637