日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【解析】Alice and Bob_24行代码AC

發布時間:2024/2/28 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【解析】Alice and Bob_24行代码AC 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

立志用最少的代碼做最高效的表達


Alice and Bob like playing games very much.Today, they introduce a new game.

There is a polynomial like this: (a0x(20)+1) * (a1 * x(21)+1)…*(an-1 * x(2(n-1))+1). Then Alice ask Bob Q questions. In the expansion of the Polynomial, Given an integer P, please tell the coefficient of the x^P.

Can you help Bob answer these questions?

Input
The first line of the input is a number T, which means the number of the test cases.

For each case, the first line contains a number n, then n numbers a0, a1, … an-1 followed in the next line. In the third line is a number Q, and then following Q numbers P.

1 <= T <= 20
1 <= n <= 50
0 <= ai <= 100
Q <= 1000
0 <= P <= 1234567898765432

Output
For each question of each test case, please output the answer module 2012.

Sample Input
1
2
2 1
2
3
4
Sample Output
2
0


解析

題意:輸入a1?ana1-ana1?an,輸入ppp,問輸出xpx^pxp次方前面的系數是多少

解析:不難看出是一道規律推導題。
寫出前三項相乘的結果:
(a0?x+1)?(a1?x2+1)?(a2?x4+1)=a0a1a2?x7+a1a2?x6+a0a2?x5+a2?x4+...(a_0*x+1)*(a_1*x^2+1)*(a_2*x^4+1)=a_0a_1a_2*x^7+a_1a_2*x^6+a_0a_2*x^5+a_2*x^4+...(a0??x+1)?(a1??x2+1)?(a2??x4+1)=a0?a1?a2??x7+a1?a2??x6+a0?a2??x5+a2??x4+...

進而推導出:xpx^pxp的系數,就是按二進制位數取1的位子上相乘的值。


#include<bits/stdc++.h> using namespace std; typedef long long gg; gg a[100]; int main() {int T; cin >> T; while(T--) {memset(a, 0, sizeof(a));int n; cin >> n; for(int i = 0; i < n; i++) cin >> a[i];int q; cin >> q;for(int i = 0; i < q; i++) {gg sum = 1, k = 0; //存放結果 gg p; cin >> p;while(p) {if(p%2 == 1) sum = (sum*a[k])%2012; k++;p /= 2;}cout << sum << '\n'; }}return 0; }

總結

以上是生活随笔為你收集整理的【解析】Alice and Bob_24行代码AC的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。