2005: [noi2010] energy collection time limit: 10 sec memory limit: 552 MB
Submit: 1653 solved: 983
[Submit] [Status] Description
The building has a rectangular place where he planted an energy plant capable of collecting sunlight. After these plants collect energy, Dong uses another energy gathering machine to gather the energy these plants collect. Tall plants are neatly planted. There are n columns, each of which has m trees, and the spacing between plants is the same. Therefore, for each plant, the building can be represented by a coordinate (x, y). The range of X is 1 to n, indicating that it is in column X, and the range of Y is 1 to M, indicates y in column X. Because the energy collection machine is large and inconvenient to move, Dong places it on a corner, and the coordinates are exactly (0, 0 ). The energy gathering machine has a certain amount of energy loss in the collection process. If a plant is connected to a power Collecting Machine with K plants on the line segment, the loss of energy is 2 k + 1. For example, when an energy collecting machine collects plants whose coordinates are (2, 4), a 3 energy loss occurs due to a plant (1, 2) in the connection segment. Note: If a plant does not have any plants on the line connecting to the energy collecting machine, the energy loss is 1. Calculate the total energy loss. The following is an example of energy collection, where n = 5, M = 4, a total of 20 plants, the Energy Loss Produced when each plant shows the energy that is collected by machines to collect its energy. In this example, a total of 36 energy losses are generated.
Input
Contains only one row, which is two integers n and M.
Output
Contains only one integer, indicating the total energy loss.
Sample input [Example input 1]
5 4
[Example input 2]
3 4
Sample output [sample output 1]
36
[Sample output 2]
20
[Data scale and Conventions]
10% of data: 1 ≤ n, m ≤ 10;
50% of data: 1 ≤ n, m ≤ 100;
80% of data: 1 ≤ n, m ≤ 1000;
90% of data: 1 ≤ n, m ≤ 10,000;
For 100% of data: 1 ≤ n, m ≤ 100,000. Question: thoughts:
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 7 typedef long long LL; 8 const int maxn = 1e5+7; 9 bool s[maxn];10 int prime[maxn],len = 0;11 int mu[maxn];12 int sum1[maxn];13 void init()14 {15 memset(s,true,sizeof(s));16 mu[1] = 1;17 for(int i=2;i<maxn;i++)18 {19 if(s[i] == true)20 {21 prime[++len] = i;22 mu[i] = -1;23 }24 for(int j=1;j<=len && (long long)prime[j]*i<maxn;j++)25 {26 s[i*prime[j]] = false;27 if(i%prime[j]!=0)28 mu[i*prime[j]] = -mu[i];29 else30 {31 mu[i*prime[j]] = 0;32 break;33 }34 }35 }36 for(int i=1;i<maxn;i++)37 sum1[i] = sum1[i-1]+mu[i];38 }39 40 int main()41 {42 int a,b,n,m;43 init();44 while(scanf("%d%d",&a,&b)>0)45 {46 if(a>b) swap(a,b);47 LL sum = 0;48 LL ans ;49 for(int i=1;i<=a;i++)50 {51 n = a/i;52 m = b/i;53 ans = 0;54 if(n>m) swap(n,m);55 for(int j=1,la = 0; j<=n;j=la+1)56 {57 la = min(n/(n/j),m/(m/j));58 ans = ans + (long long)(sum1[la] - sum1[j-1])*(n/j)*(m/j);59 }60 sum = sum + (long long)ans*(2*i-1);61 }62 printf("%lld\n",sum);63 }64 return 0;65 }
1 //package ttMain; 2 3 import java.math.BigDecimal; 4 import java.math.BigInteger; 5 import java.util.Scanner; 6 7 public class Main{ 8 9 static long f[] = new long[100003];10 public static void main(String[] args) {11 Scanner cin = new Scanner(System.in);12 int a = cin.nextInt();13 int b = cin.nextInt();14 fun(a,b);15 int tmp = min(a,b);16 BigInteger sum = BigInteger.ZERO;17 for(int i=1;i<=tmp;i++)18 sum = sum.add(BigInteger.valueOf(f[i]).multiply(BigInteger.valueOf(i)));19 sum = sum.multiply(BigInteger.valueOf(2));20 BigInteger fone = BigInteger.valueOf(-1);21 fone = fone.multiply(BigInteger.valueOf(a).multiply(BigInteger.valueOf(b)));22 sum = sum.add(fone);23 System.out.println(sum);24 }25 private static int min(int a, int b) {26 return a>b? b:a;27 }28 private static void fun(int n,int m) {29 if(n>m) {30 int tmp = n;31 n =m;32 m = tmp;33 }34 for(int i=1;i<=n;i++) f[i] = 0;35 for(int i=n;i>=1;i--)36 {37 f[i] = (long)(n/i)*(long)(m/i);38 for(int j=i+i;j<=n;j=j+i)39 f[i] = f[i] - f[j];40 }41 }42 }
Bzoj 2005: [noi2010] energy collection