山東省第四屆acm解題報告(部分)

尋找&星空の孩子發表於2013-08-17

 

 

 

 

Rescue The Princess

 

Description

Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry  the princess set out immediately. Yet, the beast set a maze. Only if the prince find out the maze’s exit can he save the princess.

Now, here comes the problem. The maze is a dimensional plane. The beast is smart, and he hidden the princess snugly. He marked two coordinates of an equilateral triangle in the maze. The two marked coordinates are A(x1,y1) and B(x2,y2). The third coordinate C(x3,y3) is the maze’s exit. If the prince can find out the exit, he can save the princess. After the prince comes into the maze, he finds out the A(x1,y1) and B(x2,y2), but he doesn’t know where the C(x3,y3) is. The prince need your help. Can you calculate the C(x3,y3) and tell him?

Input

The first line is an integer T(1 <= T <= 100) which is the number of test cases. T test cases follow. Each test case contains two coordinates A(x1,y1) and B(x2,y2), described by four floating-point numbers x1, y1, x2, y2 ( |x1|, |y1|, |x2|, |y2| <= 1000.0). 

        Please notice that A(x1,y1) and B(x2,y2) and C(x3,y3) are in an anticlockwise direction from the equilateral triangle. And coordinates A(x1,y1) and B(x2,y2) are given by anticlockwise.

Output

For each test case, you should output the coordinate of C(x3,y3), the result should be rounded to 2 decimal places in a line.

Sample Input

4
-100.00 0.00 0.00 0.00
0.00 0.00 0.00 100.00
0.00 0.00 100.00 100.00
1.00 0.00 1.866 0.50

Sample Output

(-50.00,86.60)
(-86.60,50.00)
(-36.60,136.60)
(1.00,1.00)

這道題題意,已知兩個點,求逆時針方向的正三角形的另一個點的座標;
我的思路是利用三角函式,和庫函式直接求解,
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 #define PI 3.1415926
 5 int main()
 6 {
 7     int T;
 8     double R,x[3],y[3],len;
 9     scanf("%d",&T);
10     while(T--)
11     {
12         scanf("%lf%lf%lf%lf",&x[0],&y[0],&x[1],&y[1]);
13         R=atan2(y[1]-y[0],x[1]-x[0]);
14         len=sqrt((y[1]-y[0])*(y[1]-y[0])+(x[1]-x[0])*(x[1]-x[0]));
15         x[2]=x[0]+len*cos(R+PI/3);
16         y[2]=y[0]+len*sin(R+PI/3);
17         printf("(%.2lf,%.2lf)\n",x[2],y[2]);
18     }
19     return 0;
20 }

首先,根據已知的兩個點求斜率R=tan;然後是等邊三角形的邊長 len,因為本題是逆時針旋轉的,所以可以在第一個點的基礎上再逆時針旋轉60°,若以A點為原點的話,所求點的座標為(len*cos(R+PI/3),len*sin(R+PI/3))這是相對座標,然後要記得要加上A是座標,才是絕對座標;

 

下面是第二種解法:

 

 

向量的旋轉

實際做題中我們可能會遇到很多有關及計算幾何的問題,其中有一類問題就是向量的旋轉問題,下面我們來具體探討一下有關旋轉的問題

首先我們先把問題簡化一下,我們先研究一個點繞另一個點旋轉一定角度的問題。已知A點座標(x1,y1),B點座標(x2,y2),我們需要求得A點繞著B點旋轉θ度後的位置。

A點繞B點旋轉θ角度後得到的點,問題是我們要如何才能得到A' 點的座標。(向逆時針方向旋轉角度正,反之為負)研究一個點繞另一個點旋轉的問題,我們可以先簡化為一個點繞原點旋轉的問題,這樣比較方便我們的研究。之後我們可以將結論推廣到一般的形式上。

令B是原點,我們先以A點向逆時針旋轉為例,我們過A' 做AB的垂線,交AB於C,過C做x軸的平行線交過A' 做x軸的垂線於D。過點C做x軸的垂線交x軸於點E。

令A的座標(x,y),A' 座標(x1,y1),B的座標(0,0)。我們可以輕鬆的獲取AB的長度,而且顯而易見A' B長度等於AB。假設我們已知θ角的大小那麼我們可以很快求出BC和A' C的長度。BC=A' B x cosθ,A' C=A' B x sinθ。

因為∠A' CB和∠DCE為直角(顯然的結論),則∠A' CD +∠DCB =∠ECD +∠DCB=90度。

則∠A' CD=∠ECD,∠A' DC=∠CEB=90度,因此可以推斷⊿CA' D ∽⊿CBE。由此可以退出的結論有:

BC/BE=A' C/A' D和BC/CE=A' C/CD

當然了DC和A' D都是未知量,需要我們求解,但是我們卻可以通過求出C點座標和E點座標間接獲得A' C和CD的長度。我們應該利用相似的知識求解C點座標。

C點橫座標等於:((|AB| x cosθ) / |AB|) * x = x*cosθ

C點縱座標等於:((|AB| x cosθ) / |AB|) * y = y*cosθ

則CE和BE的的長度都可以確定。

我們可以通過相⊿CA' D ∽⊿CBE得出:

AD =  x * sinθ         DC = y * sinθ

那麼接下來很容易就可以得出

x1 =  x*cosθ- y * sinθ     y1 = y*cosθ + x * sinθ

則A' 的座標為(x*cosθ- y * sinθ, y*cosθ + x * sinθ)

我們可以這樣認為:對於任意點A(x,y),A非原點,繞原點旋轉θ角後點的座標為:(x*cosθ- y * sinθ, y*cosθ + x * sinθ)

接下來我們對這個結論進行一下簡單的推廣,對於任意兩個不同的點A和B(對於求點繞另一個點旋轉後的座標時,A B重合顯然沒有太大意義),求A點繞B點旋轉θ角度後的座標,我們都可以將B點看做原點,對A和B進行平移變換,計算出的點座標後,在其橫縱座標上分別加上原B點的橫縱座標,這個座標就是A' 的座標

推廣結論:對於任意兩個不同點A和B,A繞B旋轉θ角度後的座標為:

(Δx*cosθ- Δy * sinθ+ xB, Δy*cosθ + Δx * sinθ+ yB )

注:xB、yB為B點座標。

結論的進一步推廣:對於任意非零向量AB(零向量研究意義不大),對於點C進行旋轉,我們只需求出點A和B對於點C旋轉一定角度的座標即可求出旋轉後的向量A' B' ,因為

向量旋轉後仍然是一條有向線段。同理,對於任意二維平面上的多邊形旋轉也是如此

附上學長程式碼:

 

#include <iostream>  
#include <cstdio>  
#include <cmath>  
#include <vector>  
#include <cstring>  
#include <algorithm>  
#include <string>  
#include <set>  
#include <functional>  
#include <numeric>  
#include <sstream>  
#include <stack>  
#include <map>  
#include <queue>  
  
#define CL(arr, val)    memset(arr, val, sizeof(arr))  
  
#define lc l,m,rt<<1  
#define rc m + 1,r,rt<<1|1  
#define pi acos(-1.0)  
#define ll __int64  
#define L(x)    (x) << 1  
#define R(x)    (x) << 1 | 1  
#define MID(l, r)   (l + r) >> 1  
#define Min(x, y)   (x) < (y) ? (x) : (y)  
#define Max(x, y)   (x) < (y) ? (y) : (x)  
#define E(x)        (1 << (x))  
#define iabs(x)     (x) < 0 ? -(x) : (x)  
#define OUT(x)  printf("%I64d\n", x)  
#define lowbit(x)   (x)&(-x)  
#define Read()  freopen("din.txt", "r", stdin)  
#define Write() freopen("dout.txt", "w", stdout);  
  
  
#define M 137  
#define N 22  
  
using namespace std;  
  
  
const int inf = 0x7f7f7f7f;  
const int mod = 1000000007;  
  
double xx1,yy1,xx2,yy2;  
  
int main()  
{  
//    Read();  
    int T;  
    scanf("%d",&T);  
    while (T--)  
    {  
        scanf("%lf%lf%lf%lf",&xx1,&yy1,&xx2,&yy2);  
//        printf("%.3lf %.3lf %.3lf %.3lf\n",xx1,yy1,xx2,yy2);  
        double tx = xx2 - xx1;  
        double ty = yy2 - yy1;  
  
        double x = tx*(1.0/2.0) - ty*(sqrt(3.0)/2.0) + xx1;  
        double y = ty*(1.0/2.0) + tx*(sqrt(3.0)/2.0) + yy1;  
        printf("(%.2lf,%.2lf)\n",x,y);  
    }  
    return 0;  
}
View Code

雖然自己還有點不太理解;不過先存著再說哈,,,

 

那麼下面是第二題:

Thrall’s Dream

Time Limit: 1000ms   Memory limit: 65536K 

題目描述

We never paid any heed to the ancient prophecies, like fools we clung to the old hatreds, and fought as we had for generations. Until one day the sky rained fire, and a new enemy came upon us. We stand now upon the brink of destruction, for the Reign of Chaos has come at last.

Thrall, the warchief of the Orcish Horde, all along, he led his tribe live in the fringe of Lordaeron under the human control. In a downpour night, Thrall falls into sleep in a Orc hall at Arathi Highlands, at this moment he heard a voice:

“The sands of time have run out, son of Durotan. The cries of war echo upon the winds, the remnants of the past scar the land which is besieged once again by conflict. Heroes arise to challenge fate, and lead their brethren to battle. As mortal armies rush blindly towards their doom, The Burning Shadow comes to consume us all. You must rally the Horde, and lead your people to their destiny.

I will answer all of your questions in time, young warchief. For now, rally your warriors and prepare to leave this land, cross the sea to the distant land of Kalimdor. We will speak again. ”                                                                                                                            

Thrall believes the prophesy of Blood Raven Medivh. Three days later, He and Grom Hellscream's Warsong Clan meet in the Lordaeron coast to the distant lands of Kalimdor. But the Goblin Zeppelins they take encountered storms in the middle. Thrall and Grom falling to the islands, they want to find each other and then to Kalimdor.

 

For the sake of simplicity, we assume that Thrall and Grom may fall into any islands x and y, only by Thrall to find Grom or by Grom to find Thrall. Give you the map of this island, please judge that Thrall and Gtom can meet?

輸入

    There are multiple test case in the input file, first line is a case number T. Each test case will begin with two integers N (0 <= N < 2001) and M (0 <= M < 10001), where N is the number of islands and M is number of portal. Next M lines each line contains two integers a and b, indicated there is a portal in island a that people can go from a to b by this portal. The island numbered from 1 to N.

輸出

    For each test case, your output should be in one line with “Kalimdor is just ahead” (without quotes, hereinafter the same) if Thrall and Grom can meet or “The Burning Shadow consume us all” otherwise as indicated in the sample output. 

示例輸入

2
3 2
1 2
1 3
3 2
1 2
2 3

示例輸出

Case 1: The Burning Shadow consume us all
Case 2: Kalimdor is just ahead

 

題意 :給定n個點,m條有向邊,求解任意兩點是否可能到達;

思路:列舉每個點,然後bfs就可以了

附程式碼

#include <iostream>  
#include <cstdio>  
#include <cmath>  
#include <vector>  
#include <cstring>  
#include <algorithm>  
#include <string>  
#include <set>  
#include <functional>  
#include <numeric>  
#include <sstream>  
#include <stack>  
#include <map>  
#include <queue>  
  
#define CL(arr, val)    memset(arr, val, sizeof(arr))  
  
#define lc l,m,rt<<1  
#define rc m + 1,r,rt<<1|1  
#define pi acos(-1.0)  
#define ll __int64  
#define L(x)    (x) << 1  
#define R(x)    (x) << 1 | 1  
#define MID(l, r)   (l + r) >> 1  
#define Min(x, y)   (x) < (y) ? (x) : (y)  
#define Max(x, y)   (x) < (y) ? (y) : (x)  
#define E(x)        (1 << (x))  
#define iabs(x)     (x) < 0 ? -(x) : (x)  
#define OUT(x)  printf("%I64d\n", x)  
#define lowbit(x)   (x)&(-x)  
#define Read()  freopen("din.txt", "r", stdin)  
#define Write() freopen("dout.txt", "w", stdout);  
  
  
#define M 10007  
#define N 2007  
  
using namespace std;  
  
  
const int inf = 0x7f7f7f7f;  
const int mod = 1000000007;  
  
struct node  
{  
    int v;  
    int next;  
}g[M];  
int head[N],ct;  
bool ok[N][N];  
bool vt[N];  
int n,m;  
  
void add(int u,int v)  
{  
    g[ct].v = v;  
    g[ct].next = head[u];  
    head[u] = ct++;  
}  
void bfs(int s)  
{  
    int i;  
    for (i = 1; i <= n; ++i) vt[i] = false;  
    vt[s] = true;  
    queue<int> Q;  
    Q.push(s);  
    while (!Q.empty())  
    {  
        int u = Q.front(); Q.pop();  
        for (i = head[u]; i != -1; i = g[i].next)  
        {  
            int v = g[i].v;  
            if (!vt[v])  
            {  
                vt[v] = true;  
                ok[s][v] = true;  
                Q.push(v);  
            }  
        }  
    }  
}  
int main()  
{  
//    Read();  
    int T;  
    int i,j;  
    scanf("%d",&T);  
    int cas = 1;  
    while (T--)  
    {  
       scanf("%d%d",&n,&m);  
       CL(head,-1); ct = 0;  
       int x,y;  
       for (i = 0; i < m; ++i)  
       {  
           scanf("%d%d",&x,&y);  
           add(x,y);  
       }  
       CL(ok,false);  
       for (i = 1; i <= n; ++i) ok[i][i] = true;  
       for (i = 1; i <= n; ++i) bfs(i);  
  
       bool flag = false;  
       for (i = 1; i <= n && !flag; ++i)  
       {  
           for (j = 1; j <= n && !flag; ++j)  
           {  
               if (ok[i][j] || ok[j][i]) continue;  
               else  
               {  
                   flag = true;  
                   break;  
               }  
           }  
       }  
       if (!flag) printf("Case %d: Kalimdor is just ahead\n",cas++);  
       else printf("Case %d: The Burning Shadow consume us all\n",cas++);  
    }  
    return 0;  
}
View Code

 

 

那麼下一題

A^X mod P

Crawling in process... Crawling failed Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

 

Description

It's easy for ACMer to calculate A^X mod P. Now given seven integers n, A, K, a, b, m, P, and a function f(x) which defined as following.

f(x) = K, x = 1

f(x) = (a*f(x-1) + b)%m , x > 1

Now, Your task is to calculate

( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P.

Input

In the first line there is an integer T (1 < T <= 40), which indicates the number of test cases, and then T test cases follow. A test case contains seven integers n, A, K, a, b, m, P in one line.

1 <= n <= 10^6

0 <= A, K, a, b <= 10^9

1 <= m, P <= 10^9

Output

For each case, the output format is “Case #c: ans”. 

c is the case number start from 1.

ans is the answer of this problem.

Sample Input

2
3 2 1 1 1 100 100
3 15 123 2 3 1000 107

Sample Output

Case #1: 14
Case #2: 63

題意:

題意很簡單,看題就知道。 A^X % P

思路:

在求A^X 冪時,快速冪求的話,是O(10^6*log(n)*40) = O(10^9) 肯定會超時,

我們將X轉化成 x = i*k + j。這樣先在陣列可以表示的範圍內找到一個k,然後儲存A^(1-k)的值,然後再將求出(A^k)^i 儲存在陣列裡,這樣每次求A^x,便可以通過這兩個陣列在O(1)的時間複雜度內求出來,這樣時間複雜度就變成了O(10^6*40) = O(4*10^7)了

 附程式碼:
#include <iostream>  
#include <cstdio>  
#include <cmath>  
#include <vector>  
#include <cstring>  
#include <algorithm>  
#include <string>  
#include <set>  
#include <functional>  
#include <numeric>  
#include <sstream>  
#include <stack>  
#include <map>  
#include <queue>  
  
#define CL(arr, val)    memset(arr, val, sizeof(arr))  
  
#define lc l,m,rt<<1  
#define rc m + 1,r,rt<<1|1  
#define pi acos(-1.0)  
#define ll long long  
#define L(x)    (x) << 1  
#define R(x)    (x) << 1 | 1  
#define MID(l, r)   (l + r) >> 1  
#define Min(x, y)   (x) < (y) ? (x) : (y)  
#define Max(x, y)   (x) < (y) ? (y) : (x)  
#define E(x)        (1 << (x))  
#define iabs(x)     (x) < 0 ? -(x) : (x)  
#define OUT(x)  printf("%I64d\n", x)  
#define lowbit(x)   (x)&(-x)  
#define Read()  freopen("din.txt", "r", stdin)  
#define Write() freopen("dout.txt", "w", stdout);  
  
  
#define M 33333  
#define N 31622  
using namespace std;  
const int mod = 1000000007;  
  
ll powB[M + 10];  
ll powA[N + 10];  
  
ll n, A, K, a, b, m, P;  
  
void init()  
{  
    powA[0] = 1;  
    for (int i = 1; i <= N; ++i)  
    {  
        powA[i] = powA[i - 1]*A%P;  
    }  
    ll tmp = powA[N];  
    powB[0] = 1;  
    for (int i = 1; i <= M; ++i)  
    {  
        powB[i] = powB[i - 1]*tmp%P;  
    }  
}  
void solve(int cas)  
{  
    ll fx = K;  
    ll ans = 0;  
    for (int i = 1; i <= n; ++i)  
    {  
        ans = (ans + powB[fx/N]*powA[fx%N]%P)%P;  
        fx = (a*fx + b)%m;  
    }  
    printf("Case #%d: %lld\n",cas++,ans);  
}  
int main()  
{  
    int T;  
    int cas = 1;  
    scanf("%d",&T);  
  
    while (T--)  
    {  
        cin>>n>>A>>K>>a>>b>>m>>P;  
        init();  
        solve(cas++);  
    }  
    return 0;  
}
View Code

 

 

接下來

 

Alice and Bob

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

 

Description

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

There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+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

Hint 

The expansion of the (2*x^(2^0) + 1) * (1*x^(2^1) + 1) is 1 + 2*x^1 + 1*x^2 + 2*x^3

 

 

題意:

給你一個長度為m的字串僅由小寫英文字母組成,求滿足a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an的子串的個數

思路:
首先它僅包含26個英文字母,我們只要列舉每一個位置,然後記錄每個位置左邊滿足條件的個數,右邊滿足條件的個數,最後乘起來就是了,那麼我們怎麼記錄左邊,右邊滿足的個數的呢?

dp[c]表示以字元c結尾的滿足情況的個數,dl[i]表示第i個位置左邊滿足條件的個數,dr[i]表示第i個位置右邊滿足條件的個數,

dp[c] = (dl[i] + 1);  s[i] = c; 1表示的s[i]單獨一個時滿足的情況,dl[i]表示他左邊的滿足的各種情況+s[i] 後滿足情況的。

 

#include <iostream>  
#include <cstdio>  
#include <cmath>  
#include <vector>  
#include <cstring>  
#include <algorithm>  
#include <string>  
#include <set>  
#include <functional>  
#include <numeric>  
#include <sstream>  
#include <stack>  
#include <map>  
#include <queue>  
  
#define CL(arr, val) memset(arr, val, sizeof(arr))  
  
#define lc l,m,rt<<1  
#define rc m + 1,r,rt<<1|1  
#define pi acos(-1.0)  
#define ll long long  
#define L(x)    (x) << 1  
#define R(x)    (x) << 1 | 1  
#define MID(l, r)   (l + r) >> 1  
#define Min(x, y)   (x) < (y) ? (x) : (y)  
#define Max(x, y)   (x) < (y) ? (y) : (x)  
#define E(x)        (1 << (x))  
#define iabs(x)     (x) < 0 ? -(x) : (x)  
#define OUT(x)  printf("%I64d\n", x)  
#define lowbit(x)   (x)&(-x)  
#define Read()  freopen("data.in", "r", stdin)  
#define Write() freopen("d.out", "w", stdout)  
  
  
#define M 100007  
#define N 100007  
  
using namespace std;  
  
  
const int inf = 0x7f7f7f7f;  
const int mod = 1000000007;  
int dp[27],dl[N],dr[N];  
int n;  
char ts[N];  
int s[N];  
  
int main()  
{  
//    Read();  
    int i,j;  
    while (~scanf("%d",&n))  
    {  
        scanf("%s",ts);  
        for (i = 0; i < n; ++i) s[i] = ts[i] - 'a';  
  
        CL(dp,0); CL(dl,0); CL(dr,0);  
  
        for (i = 0; i < n; ++i)  
        {  
            for (j = 0; j < s[i]; ++j) dl[i] += dp[j];  
  
            dl[i] %= 2012;  
            dp[s[i]] += (dl[i] + 1);  
            dp[s[i]] %= 2012;  
        }  
  
        CL(dp,0);  
        for (i = n - 1; i >= 0; --i)  
        {  
            for (j = 0; j < s[i]; ++j) dr[i] += dp[j];  
  
            dr[i] %= 2012;  
            dp[s[i]] += (dr[i] + 1);  
            dp[s[i]] %= 2012;  
        }  
        int ans = 0;  
        for (i = 0; i < n; ++i)  
        {  
            ans += dl[i]*dr[i];  
            ans %= 2012;  
        }  
        printf("%d\n",ans);  
    }  
    return 0;  
}
View Code


解法二:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int main()
{
    int t,n,a[60];
    int q;
    __int64 p;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%I64d",&p);
            int loc;
            int ans=1,cnt=0;
            while(p)
            {
                loc=p&1;
//              printf("%d",loc);
                if(cnt>=n)
                {
                    ans=0;
                    break;
                }
                if(loc==1)
                    ans=ans*a[cnt]%2012;
                cnt++;
                p>>=1;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}
View Code


 

下一題:

A-Number and B-Number

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

Submit Status

Description

Tom is very interested in number problem. Nowadays he is thinking of a problem about A-number and B-number.

 

A-number is a positive integer whose decimal form contains 7 or it can be divided by 7. We can write down the first 10 A-number ( a[i] is the ith A-number)

 {a[1]=7,a[2]=14,a[3]=17,a[4]=21,a[5]=27,a[6]=28,a[7]=35,a[8]=37,a[9]=42,a[10]=47};

 

B-number is Sub-sequence of A-number which contains all A-number but a[k] ( that k is a  A-number.)  Like 35, is the 7th A-number and 7 is also an A-number so the 35 ( a[7] ) is not a B-number. We also can write down the first 10 B-number.

{b[1]=7,b[2]=14,b[3]=17,b[4]=21,b[5]=27,b[6]=28,b[7]=37,b[8]=42,b[9]=47,b[10]=49};

 

Now Given an integer N, please output the Nth B-number.

Input

The input consists of multiple test cases.

For each test case, there will be a positive integer N as the description.

Output

For each test case, output an integer indicating the Nth B-number.

You can assume the result will be no more then 2^63-1.

Sample Input

1

7

100

Sample Output

7

37

470

 

 

題意:

給出一個多項式:(a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1)

輸入P,求X^p 前邊的係數。

思路:

首先P肯定是一個二進位制表示的數,並且唯一。我們只要將p轉化成二進位制數,然後乘上係數就好了。

#include <iostream>  
#include <cstdio>  
#include <cmath>  
#include <vector>  
#include <cstring>  
#include <algorithm>  
#include <string>  
#include <set>  
#include <functional>  
#include <numeric>  
#include <sstream>  
#include <stack>  
#include <map>  
#include <queue>  
   
#define CL(arr, val)    memset(arr, val, sizeof(arr))  
   
#define lc l,m,rt<<1  
#define rc m + 1,r,rt<<1|1  
#define pi acos(-1.0)  
#define ll long long  
#define L(x)    (x) << 1  
#define R(x)    (x) << 1 | 1  
#define MID(l, r)   (l + r) >> 1  
#define Min(x, y)   (x) < (y) ? (x) : (y)  
#define Max(x, y)   (x) < (y) ? (y) : (x)  
#define E(x)        (1 << (x))  
#define iabs(x)     (x) < 0 ? -(x) : (x)  
#define OUT(x)  printf("%I64d\n", x)  
#define lowbit(x)   (x)&(-x)  
#define Read()  freopen("din.txt", "r", stdin)  
#define Write() freopen("dout.txt", "w", stdout);  
   
   
#define M 137  
#define N 55  
   
using namespace std;  
   
   
const int inf = 0x7f7f7f7f;  
const int mod = 1000000007;  
   
int a[N],b[60];  
   
int main()  
{  
   int T,i;  
   int n,m;  
   scanf("%d",&T);  
   while (T--)  
   {  
       scanf("%d",&n);  
       CL(a,0);  
       for (i = 0; i < n; ++i) scanf("%d",&a[i]);  
   
       scanf("%d",&m);  
       int top;  
       ll p;  
       while (m--)  
       {  
           cin>>p; top = 0;  
           if (p == 0)  
           {  
               printf("1\n");  
               continue;  
           }  
           while (p)  
           {  
               b[top++] = p%2;  
               p /= 2;  
           }  
           int ans = 1;  
           for (i = 0; i < top; ++i)  
           {  
               if (b[i])  ans = ans*a[i]%2012;  
           }  
   
          printf("%d\n",ans);  
       }  
   }  
   return 0;  
}
View Code

 

最後一題:

 

Contest Print Server

 

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

 

Submit Status Practice SDIBT 3239

 

Description

 

In ACM/ICPC on-site contests ,3 students share 1 computer,so you can print your source code any time. Here you need to write a contest print server to handle all the requests.

 

Input

 

In the first line there is an integer T(T<=10),which indicates the number of test cases.

 

In each case,the first line contains 5 integers n,s,x,y,mod (1<=n<=100, 1<=s,x,y,mod<=10007), and n lines of requests follow. The request is like "Team_Name request p pages" (p is integer, 0<p<=10007, the length of "Team_Name" is no longer than 20), means the team "Team_Name" need p pages to print, but for some un-know reason the printer will break down when the printed pages counter reached s(s is generated by the function s=(s*x+y)%mod ) and then the counter will become 0. In the same time the last request will be reprint from the very begin if it isn't complete yet(The data guaranteed that every request will be completed in some time).

 

You can get more from the sample.

 

Output

 

Every time a request is completed or the printer is break down,you should output one line like "p pages for Team_Name",p is the number of pages you give the team "Team_Name".

 

Please note that you should print an empty line after each case

 

Sample Input

 

2

3 7 5 6 177

Team1 request 1 pages

Team2 request 5 pages

Team3 request 1 pages

3 4 5 6 177

Team1 request 1 pages

Team2 request 5 pages

Team3 request 1 pages

Sample Output

1 pages for Team1

5 pages for Team2

1 pages for Team3

1 pages for Team1

3 pages for Team2

5 pages for Team2

1 pages for Team3

 

這道題的題目意思是:印表機的列印方式,如果沒有紙了,就記下來,下次再打,沒有的時候輸出0;
具體見程式碼,是簡單題,

附程式碼:

 

#include<stdio.h>
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n,s,x,y,mod;
        scanf("%d%d%d%d%d",&n,&s,&x,&y,&mod);//n個隊提交的次數,s紙張數,
        int count,i;
        count=s;
        for(i=0;i<n;i++)
        {
            char a[50],b[20],c[20];
            int num;
            scanf("%s%s%d%s",a,b,&num,c);
            while(1)
            {
                if(num>count)
                {
                    printf("%d pages for %s\n",count,a);
                    s=(s*x+y)%mod;
                    if(s==0)//一開始為零
                        s=(s*x+y)%mod;
                    count=s;
                }
                else
                {
                    printf("%d pages for %s\n",num,a);
                    count=count-num;
                    break;
                }
            }
            
        }
        puts("");
    }
    return 0;
}

/*
2
4 7 5 6 177
Team1 request 1 pages
Team2 request 5 pages
Team3 request 1 pages
Team3 request 8 pages
3 4 5 6 177
Team1 request 1 pages
Team2 request 5 pages
Team3 request 1 pages
*/

 

 

 

 

 

 

相關文章