ACM-ICPC 2018 沈阳赛区网络预赛 D G K持续更新中

版权声明:布呗之路的守望者 https://blog.csdn.net/hypHuangYanPing/article/details/82532821
/**
G. Spare Tire
链接:https://nanti.jisuanke.com/t/31448
题意:由递推式可得到数列的第n项为n*(n+1),前n项和为n*(n+1)*(2*n+1)/6+n*(n+1)/2;
枚举m的质因子,通过容斥找出[1,n]中与m不互质的数
对于m的每个质因数组合y  可以得到最大和其不互质的数 m/y*y  最后依据容斥原理 奇加偶减冗余一下即可
*******tricks******
取余长度过长 wa....了一下午...O(1)直接ans wa
*/

#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef unsigned long long LL;

const int maxn = 1e6 + 10;
LL arr[maxn];
int p;
LL inv6,inv2;
LL quick(LL x,LL y){
    LL ans= 1;
    while(y){
        if(y&1)ans=ans*x%mod;
        y=y>>1;
        x=x*x%mod;
    }
    return ans;
}
/**
4 4
9876543 1234567
*/

LL get(LL x, LL y) {
    LL cnt = x / y;
    LL tmp=y*y%mod;
    tmp=tmp*cnt%mod*(cnt+1)%mod*(2*cnt+1)%mod*inv6;
    tmp=(tmp+y*cnt%mod*(cnt+1)%mod*inv2)%mod;
    return tmp;
}

void getp(LL n) {  //分解质因子
    p = 0;
    for(int i = 2; i * i <= n; i++) {
        if(n % i == 0) {
            arr[p++] = i;
            while(n % i == 0) n /= i;
        }
    }
    if(n > 1) arr[p++] = n;
}

int main() {
    LL n,m;
    inv6=quick(6ll,mod-2);
    inv2=quick(2ll,mod-2);
    while(~scanf("%lld %lld", &n,&m)) {
        getp(m);
        LL sum = n * (n + 1)%mod * (2 * n + 1)%mod*inv6%mod+n*(n+1)%mod*inv2%mod;
        LL ans = 0;
        for(int i = 1; i < (1 << p); i++) { //状压
            LL res = 0, cnt = 1;
            for(int j = 0; j < p; j++) {
                if(i & (1 << j)) {
                    cnt *= arr[j];
                    res++;
                }
            }
            if(res & 1) ans =(ans+get(n, cnt))%mod; //容斥
            else ans = (ans-get(n, cnt)+mod)%mod;
        }
        sum=(sum-ans+mod)%mod;
        printf("%lld\n",sum);
    }
    return 0;
}

容斥的第二种写法;

LL ans;
void dfs(int step,int flag,int tmp,ll x){
	if(step==p){
		ans=(ans+mod+flag*get(x,tmp))%mod;
		return ;
	}
	dfs(step+1,flag,tmp,x);
	dfs(step+1,-flag,lcm(tmp,arr[step]),x);
}

int main() {
	LL n,m;
    inv6=quick(6ll,mod-2);
    inv2=quick(2ll,mod-2);
    while(~scanf("%lld %lld", &n,&m)) {
        getp(m);
	    ans = 0;
        dfs(0,1,1,n);
        printf("%lld\n",ans);
    }
    return 0;
}

/***
 K. Supreme Number
链接:https://nanti.jisuanke.com/t/31452
题意:求1-->n最大的数&&该数的子序列组合后为质数;
打表即可;;
******tricks****
基本语法;......
*/

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int a[100]={2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};

int main (){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t;cin>>t;
	string str;
	for(int cas=1;cas<=t;cas++){
		cin>>str;
		int len=str.length();
		if(len>=4) printf("Case #%d: 317\n",cas);
		else {
			int ans=0,len=str.length();
			for(int i=0;i<len;i++) ans=ans*10+(str[i]-'0');
			if(ans>=317) { printf("Case #%d: 317\n",cas);}
			else for(int i=0;i<=19;i++){
				if(a[i]>ans) { printf("Case #%d: %d\n",cas,a[i-1]);break;}
				else if(a[i]==ans) { printf("Case #%d: %d\n",cas,a[i]);break;}
			}
		}
	}
	return 0;
}
/**
D. Made In Heaven
链接:https://nanti.jisuanke.com/t/31445
题意:是否存在第k短路且长度是否小于等于T.
思路:*A + dijstra +堆优化 模板题,原题;
*/

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int maxn = 10000 + 5;
const int INF = 0x3f3f3f3f;
int s, t, k;

bool vis[maxn];
int dist[maxn];

struct Node {
    int v, c;
    Node (int _v = 0, int _c = 0) : v(_v), c(_c) {}
    bool operator < (const Node &rhs) const {
        return c + dist[v] > rhs.c + dist[rhs.v];
    }
};

struct Edge {
    int v, cost;
    Edge (int _v = 0, int _cost = 0) : v(_v), cost(_cost) {}
};

vector<Edge>E[maxn], revE[maxn];

void Dijkstra(int n, int s) {
    memset(vis, false, sizeof(vis));
    for (int i = 1; i <= n; i++) dist[i] = INF;
    priority_queue<Node>que;
    dist[s] = 0;
    que.push(Node(s, 0));
    while (!que.empty()) {
        Node tep = que.top(); que.pop();
        int u = tep.v;
        if (vis[u]) continue;
        vis[u] = true;
        for (int i = 0; i < (int)E[u].size(); i++) {
            int v = E[u][i].v;
            int cost = E[u][i].cost;
            if (!vis[v] && dist[v] > dist[u] + cost) {
                dist[v] = dist[u] + cost;
                que.push(Node(v, dist[v]));
            }
        }
    }
}

int astar(int s) {
    priority_queue<Node> que;
    que.push(Node(s, 0)); k--;
    while (!que.empty()) {
        Node pre = que.top(); que.pop();
        int u = pre.v;
        if (u == t) {
            if (k) k--;
            else return pre.c;
        }
        for (int i = 0; i < (int)revE[u].size(); i++) {
            int v = revE[u][i].v;
            int c = revE[u][i].cost;
            que.push(Node(v, pre.c + c));
        }
    }
    return -1;
}

void addedge(int u, int v, int w) {
    revE[u].push_back(Edge(v, w));
    E[v].push_back(Edge(u, w));
}

int main() {
    int n, m, u, v, w,yyy;
    while (scanf("%d%d", &n, &m) != EOF) {
        for (int i = 0; i <= n; i++) {
            E[i].clear();
            revE[i].clear();
        }
        scanf("%d%d%d%d", &s, &t, &k,&yyy);
        for (int i = 0; i < m; i++) {
            scanf("%d%d%d", &u, &v, &w);
            addedge(u, v, w);
        }

        Dijkstra(n, t);
        if (dist[s] == INF) {
            puts("Whitesnake!");
            continue;
        }
        if (s == t) k++;
        if(astar(s)<=yyy)puts("yareyaredawa");
        else puts("Whitesnake!");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hypHuangYanPing/article/details/82532821