ACM-ICPC 2018 焦作赛区网络预赛G Give Candies(欧拉降幂)

题意:给你n个东西,叫你把n分成任意段,这样的分法有几种(例如3:1 1 1,1 2,2 1,3 ;所以3共有4种),n最多有1e5位,答案取模p = 1e9+7

思路:就是往n个东西中间插任意个板子,所以最多能插n - 1个,所以答案为2^(n - 1) % p。直接套用模板

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
#define N 1000100
using namespace std;
char b[N];
ll p[N];
ll a, c;
ll quick(ll a, ll b){ //快速幂
    ll k = 1;
    while(b){
        if(b%2==1){
            k = k*a;
            k %=c;
        }
        a = a*a%c;
        b /=2;
    }
    return k;
}
void priem(){
    memset(p, 0, sizeof(p));
    ll i, j;
    p[1] = 1;
    for(i=2; i<=sqrt(N); i++){
        for(j=2; j<=N/i; j++)
            p[i*j] = 1;
    }
}
ll ola(ll n){ //欧拉函数
    ll i, j, r, aa;
    r = n;
    aa = n;
    for(i=2; i<=sqrt(n); i++){
        if(!p[i]){
            if(aa%i==0){
                r = r/i*(i-1);
                while(aa%i==0)
                    aa /= i;
            }
        }
    }
    if(aa>1)
        r = r/aa*(aa-1);
    return r;
}
int main(){
    ll d, i, j;
    priem();
    a=2;c=1e9+7;
    int T;
    cin>>T;
    while(T--){
        scanf("%s",b);
        b[strlen(b)-1]--;
        ll l = strlen(b);
        ll B=0;
        ll oc = ola(c);
      //  cout<<"oc = "<<oc<<endl;
        for(i=0; i<l; i++){
            B = B*10+b[i]-‘0‘;
            if(B>oc)
                break;
        }
        //cout<<i<<endl;
        if(i==l)
            d = quick(a,B);
        else{
            B=0;
            for(i=0; i<l; i++){ //降幂
                B = (B*10+b[i]-‘0‘)%oc;
            }
            d = quick(a,B+oc);
        }
      //  printf("B= %I64d\n",B);
        printf("%d\n",d);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Fy1999/p/9652191.html

时间: 2024-08-01 06:59:20

ACM-ICPC 2018 焦作赛区网络预赛G Give Candies(欧拉降幂)的相关文章

【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies

G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1...N) and each time a child is inv

ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies (打表找规律+快速幂)

题目链接:https://nanti.jisuanke.com/t/31716 题目大意:有n个孩子和n个糖果,现在让n个孩子排成一列,一个一个发糖果,每个孩子随机挑选x个糖果给他,x>=1,直到无糖果剩余为止.给出数字n,问有多少种分发糖果的方法. 样例输入 复制 1 4 样例输出 复制 8 解题思路:我们可以这样想,一个糖果的话,应该是只有1种方法记为x1,如果是两个糖果的话,有两种方法即为x2,分别为(1,1)和(2),从中我们可以想到如果n个糖果的话,就可以分为第n个人取1个的话就有x(

ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring wonderful substring when the times it appears in that string is between AA and BB (A \le times \le BA≤times≤B). Can you calculate the number of wonderful

ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse

A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to esca

ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 

ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}ith kind of ship is 2^{C[i]} - 12C[i]?1. How many different schemes there are if you want to use thes

ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat

分析 除了树剖没想到其他解法. 用线段树维护区间和,同时针对修改区间修改操作建立两个lazy标记,一个是\(lazy_{mul}\),另一个是\(lazy_{add}\),代表区间里的数都需要先乘以\(lazy_{mul}\),再加上\(lazy_{add}\).如果一个区间需要被重复标记,那么我们可以先把新的lazy标记施加在原有的lazy上. 如果是区间乘以val,那么就可以$ lazy_{mul}=lazy_{mul} \times val,lazy_{add}=lazy_{add}\ti

ACM-ICPC 2018 焦作赛区网络预赛 E Jiu Yuan Wants to Eat (树链剖分+线段树)

题目链接:https://nanti.jisuanke.com/t/31714 题意:给你一棵树,初始全为0,有四种操作: 1.u-v乘x    2.u-v加x   3. u-v取反  4.询问u-v的和 思路: 除去第三个操作就是很简单的树链剖分+线段树多重标记下放,所以我们只要考虑怎么维护第三个操作就好了, 由题目给的取反可知:!x =  (2^64-1) - x;   但是这样维护还是很麻烦,因为这道题是对2^64取模的,我们可以 尝试把这个式子转换成只有加法和乘法的,这样就可以将其和前面

ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)

https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了..导致不断超时,我太弱了.每次询问符合要求的最左边的点. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #i