南京网络赛题解 2018
J. Sum
A square-free integer is an integer which is indivisible by any square number except 111. For example, 6=2⋅36 = 2 \cdot 36=2⋅3 is square-free, but 12=22⋅312 = 2^2 \cdot 312=22⋅3 is not, because 222^222 is a square number. Some integers could be decomposed into product of two square-free integers, there may be more than one decomposition ways. For example, 6=1⋅6=6⋅1=2⋅3=3⋅2,n=ab6 = 1\cdot 6=6 \cdot 1=2\cdot 3=3\cdot 2, n=ab6=1⋅6=6⋅1=2⋅3=3⋅2,n=ab and n=ban=ban=ba are considered different if a̸=ba \not = ba̸=b. f(n)f(n)f(n) is the number of decomposition ways that n=abn=abn=ab such that aaa and bbb are square-free integers. The problem is calculating ∑i=1nf(i)\sum_{i = 1}^nf(i)∑i=1nf(i).
Input
The first line contains an integer T(T≤20)T(T\le 20)T(T≤20), denoting the number of test cases.
For each test case, there first line has a integer n(n≤2⋅107)n(n \le 2\cdot 10^7)n(n≤2⋅107).
Output
For each test case, print the answer ∑i=1nf(i)\sum_{i = 1}^n f(i)∑i=1nf(i).
Hint
∑i=18f(i)=f(1)+⋯+f(8)\sum_{i = 1}^8 f(i)=f(1)+ \cdots +f(8)∑i=18f(i)=f(1)+⋯+f(8)
=1+2+2+1+2+4+2+0=14=1+2+2+1+2+4+2+0=14=1+2+2+1+2+4+2+0=14.
样例输入
2
5
8
样例输出
8
14
题目来源
解析:题目求和1-n的各个数的非平方因子的和,与质数有关,或者用积性函数线性筛。
提交验证代码处:https://nanti.jisuanke.com/t/30999
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 5 int t; 6 LL n; 7 const int maxn = 22000000; 8 bool tag[maxn]; 9 int p[maxn/10]; 10 int cnt; 11 LL sum[maxn]; 12 void GetPrime() 13 { 14 cnt = 0; 15 sum[1] = 1; 16 for(int i = 2; i < maxn; i++) 17 { 18 if(!tag[i]) 19 { 20 p[cnt++] = i; 21 sum[i] = 2; 22 } 23 for(int j = 0; j < cnt && p[j] * i < maxn; j++) 24 { 25 tag[i * p[j]] = 1; 26 if(i % p[j] == 0) 27 { 28 int qq = 0, xx = i; 29 LL v = 1; 30 while (xx%p[j]==0) 31 { 32 qq ++; 33 v *= p[j]; 34 xx/=p[j]; 35 } 36 v *= p[j]; 37 if (qq >= 2) 38 { 39 sum[i*p[j]] = 0; 40 } 41 else if(qq == 1) 42 { 43 sum[i*p[j]] = sum[i] / 2; 44 } 45 break; 46 } 47 sum[i*p[j]] = sum[i] * 2; 48 } 49 } 50 for (int i = 1; i < maxn; i++) 51 { 52 sum[i] += sum[i-1]; 53 } 54 } 55 int main() 56 { 57 GetPrime(); 58 cin >> t; 59 while(t--) 60 { 61 scanf("%lld", &n); 62 printf("%lld\n", sum[n]); 63 } 64 return 0; 65 }
L. Magical Girl Haze
-
14.33%
-
1000ms
-
262144K
There are NNN cities in the country, and MMM directional roads from uuu to v(1≤u,v≤n)v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance cic_ici. Haze is a Magical Girl that lives in City 111, she can choose no more than KKK roads and make their distances become 000. Now she wants to go to City NNN, please help her calculate the minimum distance.
Input
The first line has one integer T(1≤T≤5)T(1 \le T\le 5)T(1≤T≤5), then following TTT cases.
For each test case, the first line has three integers N,MN, MN,M and KKK.
Then the following MMM lines each line has three integers, describe a road, Ui,Vi,CiU_i, V_i, C_iUi,Vi,Ci. There might be multiple edges between uuu and vvv.
It is guaranteed that N≤100000,M≤200000,K≤10N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0≤Ci≤1e90 \le C_i \le 1e90≤Ci≤1e9. There is at least one path between City 111 and City NNN.
Output
For each test case, print the minimum distance.
样例输入
1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2
样例输出
3
题目来源
代码测试地址:https://nanti.jisuanke.com/t/31001
题目大意:把图中:1-n的路中的所有路的任意小于等于k条边变为0,然后求1-n的最短路径长度。
题目思路:dijkstra+优先队列
#include<bits/stdc++.h> using namespace std; #define ll long long const int maxn=1e5+5; const ll inf=1e17; struct node { ll dis; int num,pos; node() {} node(ll dis,int num,int pos):dis(dis),num(num),pos(pos) {} bool operator< (const node& a)const { //if(dis==a.dis) // return num>a.num; return dis>a.dis; } }; struct edge { int to,next; ll z; } e[maxn*2]; int head[maxn],cnt; void add(int x,int y,ll w) { e[cnt].to=y; e[cnt].z=w; e[cnt].next=head[x]; head[x]=cnt++; } ll dist[maxn][12],vis[maxn][12]; int n,m,k; void dijkstra() { priority_queue<node>Q; Q.push(node(0,0,1)); while(!Q.empty()) { node v=Q.top(); Q.pop(); if(vis[v.pos][v.num]) continue; vis[v.pos][v.num]=1; for(int i=head[v.pos]; ~i; i=e[i].next) { int ne=e[i].to; if(dist[ne][v.num]>v.dis+e[i].z) { dist[ne][v.num]=v.dis+e[i].z; Q.push(node(dist[ne][v.num],v.num,ne)); } if(v.num<k && v.dis<dist[ne][v.num+1]) { dist[ne][v.num+1]=v.dis; Q.push(node(v.dis,v.num+1,ne)); } } } } void init() { for(int i=0; i<maxn; i++) { head[i]=-1; for(int j=0; j<=10; j++) dist[i][j]=inf,vis[i][j]=0; } cnt=0; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); int x,y; ll z; init(); for(int i=1; i<=m; i++) { scanf("%d%d%lld",&x,&y,&z); add(x,y,z); } dist[1][0]=0; dijkstra(); ll ans=inf; for(int i=0; i<=k; i++) ans=min(ans,dist[n][i]); printf("%lld\n",ans); } return 0; }