HDU 2254 Olympics (number theory + matrix)

Source: Internet
Author: User
Tags mul

The question is not explained in Chinese...

Note that in discrete mathematics, the number of paths with a path length of 1 between all vertices is indicated by the adjacent matrix A of the directed graph. A ^ n indicates the number of paths with a path length of N, therefore, it is required that the values of two points be (a ^ T1 )~ (A ^ T2.

Olympics Time Limit: 1000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 2251 accepted submission (s): 572


Problem description Beijing ushered in the first Olympics, and our cheers sounded throughout the land of China, so this year's Olympic gold medal day up!
Ji gaiz sat in the nest, shaking a small fan in his hand, watching the joy, was deeply touched by the tenacious fighting spirit of our athletes. Anyway, I had a lot of money, and he said to himself, I also come to host an olympic games to see who is more popular. However, his Olympic Games are very special:
1. The participants must be Chinese;
2. At least the addition operation will be performed (because you need to calculate the number of gold plates you have obtained)
He knows that there are many places of interest in China. He knows that he cannot play all places in the T1 to t2 days, so he decided to designate two places V1, V2, if the contestant can calculate the total number of steps from V1 to v2 in the T1 to t2 days (including t1 and t2) (each road takes one day, and cannot stay in a city, and when T1 = 0, the number of walk is 0), then he will get the corresponding number of gold medals, the total number of cities <= 30, there can be multiple roads between two cities
Each entry is considered to be different.

Input Multiple cases, each case:
Enter n to indicate that N roads exist. 0 <n <10000
In the next n rows, two numbers are read in each row. P2 indicates that the city P1 to P2 has a road, not p2 to P1 has a road (0 <= p1, p2 <2 ^ 32)
Enter a number k to indicate that there are K contestants
In the next K rows, four data types are read in each row: V1, V2, T1, T2 (0 <= T1, T2 <10000)

Output outputs an integer for each contestant in each group of data to indicate his/her gold card count (mod 2008)

Sample Input
61 21 32 33 23 12 131 2 0 01 2 1 1004 8 3 50

Sample output
015060

#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-10///#define M 1000100#define LL __int64///#define LL long long///#define INF 0x7ffffff#define INF 0x3f3f3f3f#define PI 3.1415926535898#define zero(x) ((fabs(x)<eps)?0:x)#define mod 2008const int maxn = 210;using namespace std;struct matrix{    int f[31][31];};matrix p[10001];map<int, int>mp;matrix mul(matrix a, matrix b, int n){    matrix c;    memset(c.f, 0, sizeof(c.f));    for(int i = 0; i < n; i++)    {        for(int j = 0; j < n; j++)        {            for(int k = 0; k < n; k++) c.f[i][j] += a.f[i][k]*b.f[k][j];            c.f[i][j] %= mod;        }    }    return c;}matrix pow_mod(matrix a, int b, int n){    matrix s;    memset(s.f, 0 , sizeof(s.f));    for(int i = 0; i < n; i++) s.f[i][i] = 1;    while(b)    {        if(b&1) s = mul(s, a, n);        a = mul(a, a, n);        b >>= 1;    }    return s;}matrix Add(matrix a,matrix b, int n)  {    matrix c;    for(int i = 0; i < n; i++)    {        for(int j = 0; j < n; j++)        {            c.f[i][j] = a.f[i][j]+b.f[i][j];            c.f[i][j] %= mod;        }    }    return c;}int main(){    int n, m;    while(scanf("%d",&n)!=EOF)    {        int u, v;        int ans = 0;        mp.clear();        memset(p[0].f, 0, sizeof(p[0].f));        for(int i = 0; i < n; i++)        {            scanf("%d %d",&u, &v);            if(mp.find(u) == mp.end()) mp[u] = ans++;            if(mp.find(v) == mp.end()) mp[v] = ans++;            p[0].f[mp[u]][mp[v]] ++;        }        for(int i = 1; i < 10001; i++) p[i] = mul(p[i-1], p[0], ans);        scanf("%d",&m);        int t1, t2, v1, v2;        while(m--)        {            scanf("%d %d %d %d",&v1, &v2, &t1, &t2);            if(mp.find(v1) == mp.end() || mp.find(v2) == mp.end() || t1 == 0 && t2 == 0)            {                puts("0");                continue;            }            int sum = 0;            for(int i = t1-1; i < t2; i++) sum += p[i].f[mp[v1]][mp[v2]]%mod;            printf("%d\n",sum%mod);            ///cout<<(sum%mod)<<endl;        }    }    return 0;}


HDU 2254 Olympics (number theory + matrix)

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.