Problem A

Description:
度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串。现在麻烦来了,他忘记了原来的字符串都是什么,神奇的是他竟然记得原来那些字符串的哈希值。一个字符串的哈希值,由以下公式计算得到:

请帮助度熊计算大字符串中任意一段的哈希值是多少。

Input
多组测试数据,每组测试数据第一行是一个正整数N,代表询问的次数,第二行一个字符串,代表题目中的大字符串,接下来N行,每行包含两个正整数a和b,代表询问的起始位置以及终止位置。

1≤N≤1,000

1≤len(string)≤100,000

1≤a,b≤len(string)
Output
对于每一个询问,输出一个整数值,代表大字符串从 a 位到 b 位的子串的哈希值。
Sample Input
2
ACMlove2015
1 11
8 10
1
testMessage
1 1
Sample Output
6891
9240
88

Problem solving:
这道题的意思就是每个字符串都有一个对应的哈希值并且告诉了你求哈希值的公式,每次查询给你两个数a,b问你a到b的子串对应的哈希值。

这个不能直接暴力的,1e3*1e5的复杂度会超时。所以你需要每次存进去一个字符串就处理出每一个位置对应的子串的哈希值,有点类似于前缀和的思想。
我们定义s[i]就是你处理的从第0位到第i位的哈希值。
如果查找从a到b的子串的哈希值,直接s[b]/s[a-1]即可。
这也是用到了逆元的地方,这里是对9973取模,9973是质数,所以可以直接用快速幂求逆元,然后就很简单了。
这道题注意那个符号是连乘。

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 1e5+10;
ll xiaozhu[maxn];
const ll mod=9973;
ll poww(ll x,ll y)
{
    ll ans=1;
    while(y)
    {
        if(y&1) ans=(ans*x)%mod;
        x=(x*x)%mod;
        y/=2;
    }
    return ans;
}
int main()
{
    ll n,a,b;
    string s;
    while(cin>>n>>s)
    {
        ll x,y;
        xiaozhu[0]=1;
        for(ll i=0;i<s.size();i++)
            xiaozhu[i+1]=((s[i]-28)*xiaozhu[i])%9973;
        // for(lli=0;i<=s.size();i++)
        //     cout<<xiaozhu[i]<<"?\n";
        while(n--)
        {
            cin>>a>>b;
            cout<<(xiaozhu[b]%mod)*(poww(xiaozhu[a-1],mod-2))%mod<<endl;;
        }
    }
}

A/B

Description:
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
对应每组数据输出(A/B)%9973。
Sample Input
2
1000 53
87 123456789
Sample Output
7922
6060

Problem solving:
这道题给出了你n和b,让你求(n/b)对9973取模的值
这道题就是一个板子题。
可以用扩展gcd写,也可以用快速幂写。
Code:

#include<bits/stdc++.h>
using namespace std;
int exgcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    int r=exgcd(b,a%b,x,y);
    int t=y;
    y=x-(a/b)*y;
    x=t;
    return r;
}
int main()
{
    int n,a,b,x,y;
    cin>>n;
    while(n--)
    {
        cin>>a>>b;
        exgcd(b,9973,x,y);

        cout<<((x*a)%9973+9973)%9973<<endl;
    }
}

Fansblog

Description:
Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people visited this blog.One day, he find the visits has reached P , which is a prime number.He thinks it is a interesting fact.And he remembers that the visits had reached another prime number.He try to find out the largest prime number Q ( Q < P ) ,and get the answer of Q! Module P.But he is too busy to find out the answer. So he ask you for help. ( Q! is the product of all positive integers less than or equal to n: n! = n * (n-1) * (n-2) * (n-3) *… * 3 * 2 * 1 . For example, 4! = 4 * 3 * 2 * 1 = 24 )
Input
First line contains an number T(1<=T<=10) indicating the number of testcases.
Then T line follows, each contains a positive prime number P (1e9≤p≤1e14)
Output
For each testcase, output an integer representing the factorial of Q modulo P.
Sample Input
1
1000000007
Sample Output
328400734

Problem solving:
这道题的意思就是给了你一个素数,素数的范围是1e9到1e14。问你小于所给的素数的最大的素数的阶乘对所给的素数取模的结果。

我在分析之前先贴出来一篇巨佬的博客
川巨带你拿金牌:https://blog.csdn.net/cloudy_happy/article/details/99571628

这道题用到的知识有

  1. 逆元
  2. 素数判断(更快地素数判断(可选))
  3. 快速乘
  4. 快速幂
  5. 威尔逊定理
  6. 快速幂

首先由威尔逊定理我们可以知道如果p是素数,那么(p-1)!%p=p-1,即p-1的阶乘对p取模的结果是p-1.
若输入中给的素数是p,我们找到了小于p的最大素数是q,那么我们就可以结合威尔逊定理得出
q!\*(q+1)\*(q+2)\*.......\*(p-1) % p = p-1
并且两个相邻素数相差一般不会太大,所以说我们只需要用p-1除以从p到q的每一个数就是q的阶乘。这是用到了逆元的地方。因为是素数,所以可以直接快速幂求逆元。
为什么还会用到快速乘呢?
因为p最大是1e14,即使你再乘之前就取模,在你相乘之后也会爆long long,所以就需要用到快速幂来处理。

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool check(ll x)
{
    for(ll i=2;i<=sqrt(x);i++)
        if(x%i==0)    return 0;
    return 1;
}
ll ksc(ll a,ll b,ll p)
{
    return (a*b-(ll)((long double)a/p*b)*p+p)%p;
}
ll poww(ll a,ll b,ll p)
{
    ll ans=1;
    while(b)
    {
        if(b&1)    ans=ksc(ans,a,p);
        b/=2;
        a=ksc(a,a,p);
    }
    return ans;
}
ll inv(ll x,ll p)
{
    return poww(x,p-2,p);
}
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    ll p;
    while(n--)
    {
        cin>>p;
        ll ans=p-1;
        for(ll i=p-2;;i-=2)
        {
            if(check(i))
            {
                for(ll j=i+1;j<p;j++)
                {
                    ans=ksc(ans,inv(j,p),p);
                }
                cout<<ans%p<<endl;
                break;
            }
        }
    }
}

这里数据范围很小,但是如果n增大范围,该怎么提速呢?
我们可以优化一下素数的判断,这里有个贼快的素数判断的板子,当然也是来自我们能带你拿金牌的川巨啦

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll ksc(ll a,ll b,ll p)
{
    return (a*b-(ll)((long double)a/p*b)*p+p)%p;
}
ll prime[5] = {2, 5, 3, 233, 331};
ll qpow(ll a,ll b,ll p)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1) ans = ksc(ans,a,p);
        b >>= 1;
        a = ksc(a,a,p);
    }
    return ans;
}
bool Miller_Rabin(ll p)
{
    if(p < 2) return 0;
    if(p != 2 && p % 2 == 0) return 0;
    ll s = p - 1;
    while(! (s & 1)) s >>= 1;
    for(int i = 0; i < 3; ++i)
    {
        if(p == prime[i]) return 1;
        ll t = s, m = qpow(prime[i], s, p);
        while(t != p - 1 && m != 1 && m != p - 1)
        {
            m = ksc(m, m, p);
            t <<= 1;
        }
        if(m != p - 1 && !(t & 1)) return 0;
    }
    return 1;
}
ll inv(ll x,ll p)
{
    return qpow(x,p-2,p);
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cout<<(12 %(-7))<<endl;
    cin>>t;
    while (t --)
    {
        ll p;
        cin>>p;
        ll ans = p-1;
        for (ll x = p - 2;;x -= 2)
        {
            if (Miller_Rabin(x))
            {
                for (ll i = x + 1;i < p;i ++)
                    ans = ksc(ans,inv(i,p),p);
                cout<<ans<<'\n';
                break;
            }
        }
    }
    return 0;
}

这里直接贴出来他的代码啦啦啦。

Romantic

Description:
The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You.
................................Write in English class by yifenfei
Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead.
Sample Input
77 51
10 44
34 79
Sample Output
2 -3
sorry
7 -3
Problem solving:
这个题就是给你a,b,让你求ax+by=1的时候的x和y,x还必须是非负整数。如果没有的话就输出sorry

直接用扩展gcd求就可以啦
Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    ll r=exgcd(b,a%b,x,y),t=y;
    y=x-(a/b)*y,x=t;
    return r;
}
int main()
{
    ll a,b;
    while(cin>>a>>b)
    {
        ll g=__gcd(a,b);
        ll x,y;
        exgcd(a,b,x,y);
        if(a*x+b*y!=1)
        {
              puts("sorry");
              continue;
        }
        if(b<0) b=-b;
        x%=b;
        while(x<0)
        {
            x+=b;
        }
        y=(1-a*x)/b;
     cout<<x<<" "<<y<<endl;
    }
}

扩展gcd我也不是很懂,但是套板子就行了嘛。

Integer Divisibility

Description:
If an integer is not divisible by 2 or 5, some multiple of that number in decimal notation is a sequence of only a digit. Now you are given the number and the only allowable digit, you should report the number of digits of such multiple.

For example you have to find a multiple of 3 which contains only 1's. Then the result is 3 because is 111 (3-digit) divisible by 3. Similarly if you are finding some multiple of 7 which contains only 3's then, the result is 6, because 333333 is divisible by 7.

Input
Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case will contain two integers n (0 < n ≤ 1e6 and n will not be divisible by 2 or 5) and the allowable digit (1 ≤ digit ≤ 9).

Output
For each case, print the case number and the number of digits of such multiple. If several solutions are there; report the minimum one.

Sample Input
3
3 1
7 3
9901 1

Sample Output
Case 1: 3
Case 2: 6
Case 3: 12

Problem solving:
翻译:
如果整数不能被2或5整除,则十进制表示法中该数字的某个倍数只是一个数字的序列。现在您将获得数字和唯一允许的数字,您应该报告此数字的位数。
例如,您必须找到仅包含1的3的倍数。然后结果是3,因为111(3位)可被3整除。同样,如果你发现7的多数只包含3的那么,结果是6,因为333333可以被7整除。
翻译来自:https://blog.csdn.net/A_B_C_D_E______/article/details/81205687

所以可以用大数取模来写,这个是真的不太好想。
假如输入时a,b
你每次给当前数加上一个a,答案加1,然后对b取模,直接当前值为0,退出循环。
可以看一下代码实现,还是很清晰的

Code:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
int main()
{
    int n;
    ll a,b;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a>>b;
        int ans=0,mid=0;
        ll p=1;
        while(p)
        {
            mid=(mid*10+b)%a;
            ans++;
            p=mid;
        }
        printf("Case %d: %d\n",i,ans);
    }
}

Large Division

Description:
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if there exists an integer c such that a = b * c.

Input
Input starts with an integer T (≤ 525), denoting the number of test cases.

Each case starts with a line containing two integers a (-10200 ≤ a ≤ 10200) and b (|b| > 0, b fits into a 32 bit signed integer). Numbers will not contain leading zeroes.

Output
For each case, print the case number first. Then print 'divisible' if a is divisible by b. Otherwise print 'not divisible'.

Sample Input
6
101 101
0 67
-101 101
7678123668327637674887634 101
11010000000000000000 256
-202202202202000202202202 -101

Sample Output
Case 1: divisible
Case 2: divisible
Case 3: divisible
Case 4: not divisible
Case 5: divisible
Case 6: divisible

Problem solving:
这道题就是给你两个数a,b,问你a能否整除b,但是数据范围很大,所以要用到大数取模,还是很巧妙地

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,a;
    string s;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>s>>a;
        printf("Case %d: ",i);
        ll mid=0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='-')   continue;
            mid=(mid*10+s[i]-'0')%a;
        }
        if(mid) puts("not divisible");
        else    puts("divisible");
    }
}

青蛙的约会

Description:
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具***置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
Input
输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。
Output
输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"
Sample Input
1 2 3 4 5
Sample Output
4
Problem solving:
暂无
Code:

Candy Distribution

Description:
Kids like candies, so much that they start beating each other if the candies are not fairly distributed. So on your next party, you better start thinking before you buy the candies.

If there are K kids, we of course need K⋅X candies for a fair distribution, where X is a positive natural number. But we learned that always at least one kid looses one candy, so better be prepared with exactly one spare candy, resulting in (K⋅X)+1 candies.

Usually, the candies are packed into bags with a fixed number of candies C. We will buy some of these bags so that the above constraints are fulfilled.

Input
The first line gives the number of test cases t (0<t<100). Each test case is specified by two integers K and C on a single line, where K is the number of kids and C the number of candies in one bag (1≤K,C≤109). As you money is limited, you will never buy more than 109 candy bags.

Output
For each test case, print one line. If there is no such number of candy bugs to fulfill the above constraints, print “IMPOSSIBLE” instead. Otherwise print the number of candy bags, you want to buy. If there is more than one solution, any will do.

Sample Input 1 Sample Output 1
5
10 5
10 7
1337 23
123454321 42
999999937 142857133
IMPOSSIBLE
3
872
14696943
166666655

Problem solving:
这道题就是给两个数,第一个数代表人数,第二个数代表每篮子放的糖果个数,问你需要买几蓝可以给每个人分的糖果数一样并且还剩余一个。

其实就是让你求ax-by=1的时候x的值。
{a代表的是每蓝糖果的个数,x代表的是买了几蓝,b代表的是热人数,y代表的是每个人分到的糖果的个数}

用扩展gcd就可以求出来。

还是对扩展gcd不够理解所以这道题理解起来和做起来都有点吃力8,扩展gcd啊,嘤嘤嘤。

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if (!b)
    {
        x = 1,y = 0;
        return a;
    }
    int r = exgcd(b,a%b,x,y);
    int tmp = y;
    y = x - (a / b) * y;
    x = tmp;
    return r;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        ll x,y;
        cin>>a>>b;
        ll mid=exgcd(a,b,x,y);
        if(mid!=1)    puts("IMPOSSIBLE");
        else
        {
            if(b==1)
            {
                cout<<a+1<<endl;
                continue;
            }
            while(y<=0)    y+=a;
            cout<<y<<endl;
        }
    }
    return 0;
}